-2

I have an array of a class A. Class A contains 2 properties as Weight and Date.

var array = [
              A{Weight: 50, Date: 01/01/2009}, 
              A{Weight: 55, Date: 01/02/2009}, 
              A{Weight: 60, Date: 01/03/2009},... 
]

How can I convert the above array to something like this:

var array = [
              {Weight: 50, Date: 01/01/2009}, 
              {Weight: 55, Date: 01/02/2009}, 
              {Weight: 60, Date: 01/03/2009},... 
]
Mohammad Shadmehr
  • 675
  • 11
  • 26

1 Answers1

0

Your code isn't valid JavaScript code. I did a little restructuring and here you have one way of achieving what you need, meaning it can be done in different ways, but since your code is not valid, I was restricted. I suggest going over official documentation. MDN is a great starting point. I also recommend this article. Anyways, I hope this helps you ;)

// A valid JS array would look something like this:

const array = [
               {a: {Weight: 50, Date: '01/01/2009'}}, 
               {a: {Weight: 50, Date: '01/01/2009'}}, 
               {a: {Weight: 50, Date: '01/01/2009'}}
];

// Here we define our new array that's going to contain our 'mutated' data

let newArr = [];

// Here we're going over our original array and push redefined data to our 'newArr'

array.map(x => {
  newArr.push(x.a);
});

// Now, let's see how that looks like :)

console.log(newArr);
Dženis H.
  • 7,284
  • 3
  • 25
  • 44