admin管理员组文章数量:1410697
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.
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.
Share Improve this question asked Jan 22, 2020 at 11:16 Tony STony S 5718 silver badges30 bronze badges 4- And the question/problem is? -> How do I ask a good question? – Andreas Commented Jan 22, 2020 at 11:18
-
4
Return value should be
return {name: value}
– Mohammad Usman Commented Jan 22, 2020 at 11:19 - Always check the error console. – Mitya Commented Jan 22, 2020 at 11:22
-
3
Object.values(partners).map(name => ({name}))
– Ele Commented Jan 22, 2020 at 11:25
8 Answers
Reset to default 3name
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 should use the param name
from the handler.
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 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})
}
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);
本文标签: JavaScript Object to Array of Objects with 39name39 keyStack Overflow
版权声明:本文标题:JavaScript Object to Array of Objects with 'name' key - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744308636a2599921.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论