Home > Software design >  New javascript array from json array getting only the values from the first
New javascript array from json array getting only the values from the first

Time:06-18

I have this JSON Array:

const arr = [ 
    { data: '250', name: 'john' },
    { data: '251', name: 'john' }
]:

How can i get from it, to a single new array having [250, 251] ?

I am not able to solve this.

CodePudding user response:

let arr = [ { data: '250', name: 'john' }, { data: '251', name: 'john' } ];
let result = arr.map((x) => x.data);
console.log(result);

CodePudding user response:

let arr = [
  { data: "250", name: "john" },
  { data: "251", name: "john" },
];
let result = arr.map((x) => x.data);
console.log(result);

let list = [];
for (let i = 0; i < arr.length; i  ) {
  list.push(arr[i].data);
}

console.log(list);

  • Related