简体   繁体   中英

JavaScript Object to Array of Objects with 'name' key

Here my initial object :

partners {
  0: "Proxy"
  2: "Skate"
  8: "Air"
}

I want to have this :

partners [
  0:{name: "Proxy"}
  1:{name: "Skate"}
  2:{name: "Air"}
]

I tried this without success :

var newArray = Object.values(this.initialObject).map(function (value) {
                                return { ['name']: obj[value] };
                            });

Thank you very much.

name does not need to be wrapped in [' '] as it is a key nor do you need to use obj to get the value as value is the variable passed by the function.

Try this instead:

 var partners = { 0: "Proxy", 2: "Skate", 8: "Air" } var newArray = Object.values(partners).map(function (value) { return {name: value}; }); console.log(newArray)

 const a = { 0: "Proxy", 2: "Skate", 8: "Air" } const result = Object.values(a).map((value) => { return { name: value }; }); console.log(result)

This will do it, you had to replace the obj[value] with value and replace ['name'] with simple name.

partners ={
  0: "Proxy"
  2: "Skate"
  8: "Air"
} 

var newpartners=[];
    Object.keys(partners).map(item=>{
      newpartners.push({name:partners[item]})
    })

console.log(newpartners);

Yo can use the following methods code

 var partners = { 0: "Proxy", 2: "Skate", 8: "Air" } var arr = []; var keys = Object.keys(partners); var values = Object.values(partners); keys.forEach((ele, indx) => { arr.push({ "name": values[indx] }); }) console.log(arr);

 var partners = { 0: "Proxy", 2: "Skate", 8: "Air" } var newArray = Object.values(partners).map(function (value) { return { name : value }; }); console.log(newArray);

You can try something like this, just using an array as a container for the final desired output, and loop over the original object with for ... in loop. Hope this helps. :)

var p = {
  "0": "Proxy",
  "2": "Skate",
  "8": "Air"
}
var a = [];
for(var key in p){
 if (p.hasOwnProperty(key)) {
   a.push({name: p[key]})
  }

}
console.log(a)

Another way to loop through an object is to use Object.entries method, This method return an array of arrays, each array contain the key and the value

const fruits = {
  apple: 28,
  orange: 17,
  pear: 54,
}

const entries = Object.entries(fruits)
console.log(entries)
// [
//   [apple, 28],
//   [orange, 17],
//   [pear, 54]
// ]

So, in your case you can try something like this :

const entries = Object.entries(p);
var a = [];
for (const [key, value] of entries) {

  a.push({name: value})
}

您应该使用处理程序中的参数name

Object.values(this.initialObject).map(name => ({name}));

Try This :

 var tempPartners=[];
    for (x in partners) {
    var obj=JSON.parse(`{"name":"`+partners[x]+`"}`);
    tempPartners.push(obj);
    }
console.log(tempPartners);

The output will be like

 0: {name: "Proxy"}
    1: {name: "Skate"}
    2: {name: "Air"}

You can do something like this

var partners = {"Proxy", "Skate","Air" };
var newArray = [];
for(var i = 0; i<=partners.length; i++){
    newArray.push({Name: element[i]});
}
console.log(newArray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM