Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
Not able to add JSON Data inside forEach loop using if condition on elements

Hi I am trying to add few conditional data from one JSON object to another.I am trying below code.Data is having JSON object.I am not getting any one in Data though i am getting correct value in Temp

const userData= data.rows;
    const claimsB = claimsB;
    let Temp = '';
    let Data = '';
    userData.forEach((element) => {
        if (element.value.A == claimsB.B){
            Temp = element;
            Data += Temp;
        }
        console.log(Temp);
    });
   console.log(Data);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Perhaps you want to replace

Data += Temp;

with

Data += JSON.stringify(Temp);

about 4 years ago · Juan Pablo Isaza Report

0

let Temp = ''; and let Data = ''; specify that both "Temp" and "Data" are declared as empty strings

Temp = element; is assigning the "element" object to "Temp" variable At this point, type of "Temp" is converted to Object

Data += Temp; is trying to append an object to a string. At this point, type of "Data" is still String

As per your statement, you expect "Data" to be a JSON object but that is not the case. Hence, when you try to console.log(Data) at the end of your snippet, you might be seeing [object Object]... instead of a JSON object.

Suggestion: Declare "Data" as an array and push conditionally matching elements to this. Your code will look like this:

const userData= data.rows;
let Data = [];
userData.forEach((element) => {
    if (element.value.A == claimsB.B){
        Data.push(element);
    }
    console.log(Temp);
});
console.log(Data);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!