2

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"}]}];
6
  • 1
    What have you tried yet? Commented Oct 3, 2018 at 9:42
  • There's always only 2 names? Commented Oct 3, 2018 at 9:42
  • let myarray:any[]=[];let obj1:any={};obj1[name]=cars;myarray.push(obj1) 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. Commented Oct 3, 2018 at 9:44
  • @Eliseo please answer it don't comment. Commented Oct 3, 2018 at 9:45

5 Answers 5

5
let 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);
Sign up to request clarification or add additional context in comments.

Comments

2

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));

2 Comments

Cool, didn't know you could do that!
Without dynamic code it was perfect as I wanted but, the Dynamic code is not proper because it is not an Array. @Mathyn Solution for dynamic worked for me as he is using push, So I get in Array.
1

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);
}

Comments

1

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.

2 Comments

Thanks for the help but I wanted it in an Array. Upvoted for your effort.
Oh, i see. Sorry for that. I will stay with reduce and instead of pushing it to map you can push it to array like this: jsfiddle.net/krewilone/5hsaq7eu
0

if there is always two names:

const Array = [
  { [name]: cars },
  { [name2]: cars }
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.