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

182
Views
How to call json API specific keys

can someone help me? i have json like this

[ 
 { "positive": 2, "negative": 4 }, 
 { "positive": 9, "negative": 18 }, 
 { "positive": 6, "negative": 12 } 
]

But i want to change it like this

[2, 9, 6] and [4, 18, 12]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can use map function.

var data = [ 
 { "positive": 2, "negative": 4 }, 
 { "positive": 9, "negative": 18 }, 
 { "positive": 6, "negative": 12 } 
];

const positiveList = data.map(({positive})=> positive);

const negativeList = data.map(({negative})=> negative);

console.log(positiveList);
console.log(negativeList);

Or you can merge into 1 map function.

   var data = [ 
     { "positive": 2, "negative": 4 }, 
     { "positive": 9, "negative": 18 }, 
     { "positive": 6, "negative": 12 } 
    ];

    const [positiveList, negativeList] = data.reduce(([a,b], {positive,negative})=>{ 
      a.push(positive); 
      b.push(negative); 
      return [a,b]; 
    }, [[],[]]);


    console.log(positiveList);
    console.log(negativeList);

about 4 years ago · Juan Pablo Isaza Report

0

This way - tried and tested

let data=[ 
 { "positive": 2, "negative": 4 }, 
 { "positive": 9, "negative": 18 }, 
 { "positive": 6, "negative": 12 } 
];

let new_d=[]

data.map((e,i,a)=>{Object.keys(e).map((e1,i1,a1)=>{new_d[e1]=[...new_d[e1]||[],e[e1]]})})

console.log(new_d)

enter image description here

about 4 years ago · Juan Pablo Isaza Report

0

You can do it with a simple forEach loop. Like that:

const d = [ 
 { "positive": 2, "negative": 4 }, 
 { "positive": 9, "negative": 18 }, 
 { "positive": 6, "negative": 12 } 
]

const p = []
const n = []


d.forEach(e => {
  p.push(e.positive)
  n.push(e.negative)
})

console.log("+", p)
console.log("-", n)

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!