admin管理员组文章数量:1401673
I am getting a value in string in a variable, i.e
let name = 'Vishesh';
let name2 = 'Vishesh2';
and an Array i.e
let cars = [{carName: "Mercedes"},{carName: "Volvo"},{carName:"BMW"}];
Now I want to create an Array where my Key is the name and in value, there should be cars array, i.e
Array=[{Vishesh: [{carName: "Mercedes"},{carName: "Volvo"},{carName:"BMW"}]},{Vishesh2: [{carName: "Mercedes"},{carName: "Volvo"},{carName:"BMW"}]}];
I am getting a value in string in a variable, i.e
let name = 'Vishesh';
let name2 = 'Vishesh2';
and an Array i.e
let cars = [{carName: "Mercedes"},{carName: "Volvo"},{carName:"BMW"}];
Now I want to create an Array where my Key is the name and in value, there should be cars array, i.e
Array=[{Vishesh: [{carName: "Mercedes"},{carName: "Volvo"},{carName:"BMW"}]},{Vishesh2: [{carName: "Mercedes"},{carName: "Volvo"},{carName:"BMW"}]}];
Share
Improve this question
asked Oct 3, 2018 at 9:40
VisheshVishesh
8322 gold badges8 silver badges16 bronze badges
6
- 1 What have you tried yet? – DirtyBit Commented Oct 3, 2018 at 9:42
- There's always only 2 names? – Ramya Commented Oct 3, 2018 at 9:42
- let myarray:any[]=[];let obj1:any={};obj1[name]=cars;myarray.push(obj1) – Eliseo Commented Oct 3, 2018 at 9:44
- No name1 and name2 are just an example to show it would be multiple. They are dynamic values so it's uncertain how many names you would get. – Vishesh Commented Oct 3, 2018 at 9:44
- @Eliseo please answer it don't ment. – Vishesh Commented Oct 3, 2018 at 9:45
5 Answers
Reset to default 5let name = 'Vishesh';
let name2 = 'Vishesh2';
let cars = [{carName: "Mercedes"},{carName: "Volvo"},{carName:"BMW"}];
let array = []
array.push({[name]:cars})
array.push({[name2]:cars})
console.log(array);
You can interpolate the names directly into the dictionary construction as a key.
let name = 'Vishesh';
let name2 = 'Vishesh2';
console.log([
{
[name]: [
{ carName: "Mercedes" },
{ carName: "Volvo" },
{ carName:"BMW" }
]
},
{
[name2]: [
{ carName: "Mercedes" },
{ carName: "Volvo" },
{ carName:"BMW" }
]
}
]);
But you can do this a bit more dynamically
let name = 'Vishesh';
let name2 = 'Vishesh2';
let cars = [
{ carName: "Mercedes" },
{ carName: "Volvo" },
{ carName:"BMW" }
];
function keyByNames(names, cars) {
let named = {}
names.forEach(name => {
named[name] = JSON.parse(JSON.stringify(cars))
})
return named
}
console.log(keyByNames([ name, name2 ], cars));
How about this (assuming it is only two names):
let obj1 = {};
obj1[name] = cars;
let obj2 = {};
obj2[name2] = cars;
var array = [obj1, obj2];
Do note: both objects reference the same array. Changing the array will therefore 'update' the array in two places (since it is by reference).
If however you want a dynamic set of names your code could look like this:
var names = ["Vishesh", "Vishesh2", "Vishesh3"];
var array = [];
for(var i = 0; i < names.length; i++) {
var name = names[i];
var obj = {};
obj[name] = cars;
array.push(obj);
}
using reduce function on your array of cars can be solution as well.:
let names = ["Wishes1", "Wishes2", "Wishes3"]
let cars = [{carName: "Mercedes"},{carName: "Volvo"},{carName:"BMW"}];
const withNames = (names) => (currentMapState, currentItem, currentIndex) => {
currentMapState[names[currentIndex]] = currentItem;
return currentMapState;
}
console.log(cars.reduce(withNames(names), {}));
And bonus is that withNames function is easily testable. Have a nice day.
if there is always two names:
const Array = [
{ [name]: cars },
{ [name2]: cars }
]
本文标签: javascriptCreate Array by Dynamic Keys and ValuesStack Overflow
版权声明:本文标题:javascript - Create Array by Dynamic Keys and Values - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744282585a2598726.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论