0

Let's say I have an array as follows:

types = ['Old', 'New', 'Template'];

I need to convert it into an array of objects that looks like this:

[
    {
        id: 1,
        name: 'Old'
    },
    {
        id: 2,
        name: 'New'
    },
    {
        id: 3,
        name: 'Template'
    }
]
8
  • 4
    ['Old', 'New', 'Template'].map((x,i) =>({ id: i + 1, name: x})) Commented May 30, 2017 at 10:16
  • How do I map it to a new array of objects? Commented May 30, 2017 at 10:54
  • @notyetdecided answering instead of yurzui... simply: let newArr = ['Old', 'New', 'Template'].map((x,i) =>({ id: i + 1, name: x})) :) Commented May 30, 2017 at 11:07
  • The same thing happens here too as in the answer below. The newly created array of objects contains another object in the name field and not just the string. Can anyone solve this? Commented May 30, 2017 at 11:09
  • @notyetdecided are you sure that the types array doesn't contain objects? try debugging and checking out the actual values. Commented May 30, 2017 at 11:22

2 Answers 2

2

You can use map to iterate over the original array and create new objects.

let types = ['Old', 'New', 'Template'];

let objects = types.map((value, index) => {
  return {
    id: index + 1,
    name: value
  };
})

You can check a working example here.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried this but the resulting array contains objects whose id's are proper but the name field contains another object with id and name fields and not just the string.
that can't happen unless you have something else but strings in the types array. I added a working playground link where you can see the solution working. :)
I'm sorry, you're right. I was experimenting with the wrong array while testing. Thank you.
0

The solution of above problem is the map() method of JavaScript or Type Script.

map() method creates a new array with the results of calling a provided function on every element in the calling array.

let newArray = arr.map((currentvalue,index,array)=>{
return Element of array
});
/*map() method creates a new array with the results of calling 
a provided function on every element in the calling array.*/

     let types = [
         'Old',
         'New',
         'Template'
        ];


  /*
    let newArray = arr.map((currentvalue,index,array)=>{
         return Element of array
     });
  */


   let Obj = types.map((value, i) => {
         let data = {
                 id: i + 1,
                 name: value
             };

      return data;
    });

    console.log("Obj", Obj);

Please follow following links:

TypeScript

JS-Fiddle

We can achieve the solution of above problem by for loop :

let types = [
    "One",
    "Two",
    "Three"
];

let arr = [];

for (let i = 0; i < types.length; i++){
    let data = {
        id: i + 1,
        name: types[i]
    };
    arr.push(data);
}

console.log("data", arr);

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.