0

Im trying to convert a "simple" array that looks like this:

arr = [143.17, 174.45, 55.3]

To an array of objects in this format:

 Today = new Date();
    newArr =   [ 
        { key: Today, value: 143.17 },
        { key: Today + 1, value: 174.45 },
        { key: Today + 2, value: 55.3}]

Im hoping to create a function to which I can pass the original array and have it return the new one:

objArr = function CreateObjArr(arr){


 return newArr
}

Thank you

2
  • I don't really know where to start. But you are right. I could try to convert a single value into an object first. Commented Nov 17, 2015 at 20:00
  • what you want is not a key/value structure. the key/value is so organized, that the key is the access property of an object for a value. eg { day1: 1.2, day2: 1.1, day3: 1.4 }. Commented Nov 17, 2015 at 20:21

1 Answer 1

2

You can use the Array.prototype.map function

var newArr = arr.map(function(v, i) {
  return { key: Today + i, value: v };
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.