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 }
]
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744282586a4566644.html
评论列表(0条)