How do I convert an array of integers to an array of objects?

Leo Ku

I would like to convert and array in this format

var values = [1,2,3]; 

To an array in this format

var data = [ 
  {x: 0, value: 1},
  {x: 1, value: 2},
  {x: 2, value: 3}
];
undefined

You could simply use the Array.prototype.map method:

var data = values.map(function(el, i) {
   return {
     x: i,
     value: el
   }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert an array of integers to binary?

How do I convert an array of floats into an array of unique integers?

Convert Array of objects to array of integers

How do I convert this php array into a JavaScript array of objects?

Dart: How do I convert an array of objects to an array of hashmaps?

How can I convert the array of integers to rows?

How do I convert array of Objects into one Object in JavaScript?

How do I convert an array of objects into an object (dynamically) in JavaScript

How do I convert array of objects to object in javascript?

How do I convert a named list to an array of objects

How do i convert an array of objects into 1 object in typescript

How do I convert array of Objects to one Object in JavaScript?

How do I validate an array of integers in Laravel

How do I a split a line of integers into array?

How do I convert this to an array?

I got a array contains integers like 737511, how do I convert every integer in that array into date like 2020-03-27

How to convert an array of objects in an array of integers extracting values from those objects using ramda.js?

How do I cast an array of doubles into an array of integers in Java?

How to convert an array of integers to a tree?

How to convert an array of integers to an integer

How do I cast/convert an array of objects to generic type T when T is defined as an array?

How can I convert an ARRAY of INTEGERS into a JSON dict?

How can I convert a string of numbers to an array or vector of integers in Rust?

How can I convert a array of integers of a JPG image to a JPG image?

how to convert array into objects?

How to convert an array of integers to array of enums?

How to convert an array of integers into an array of bits?

How to convert objects to Array of objects

How can I convert an array of string arrays into an array of objects?