I have an array :
const data = [{"label": "a", "value": 4}, {"label": "b", "value": 1}, {"label": "c", "value": 2}]
I want to create a new array of objects where I have to give key and value in it.
For eg,:
const newArr = [{1,4},{2,1},{3,2}]
here first item of an object is the key, which has to increase as the number of objects increase, and second item is the value
from data
.
CodePudding user response:
const data = [{"label": "a", "value": 4}, {"label": "b", "value": 1}, {"label": "c", "value": 2}];
const out = data.map((item, index) => [index 1, item.value]);
console.log(out);
CodePudding user response:
const data = [{"label": "a", "value": 4}, {"label": "b", "value": 1}, {"label": "c", "value": 2} answer
CodePudding user response:
To push an object into an array, call the push() method, passing it the object as a parameter. For example, arr.push({name: 'Tom'}) pushes the object into the array. The push method adds one or more elements to the end of the array.
index.js
let arr = [];
const obj = {name: 'Tom'};
arr.push(obj);
console.log(arr); //