Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add property/index to each element in an array

Tags:

javascript

I call a webservice that returns an array of objects:

$.ajax({
  dataType: "json",
  url: "WebServices/FetchMenu.aspx?P=0&L=2&mt=1",
  //data: data,
  success: function (data) {
    var foo = data;
  }
});

This is the response:

[
    {
        color: "red",
        value: "#f00"
    },
    {
        color: "green",
        value: "#0f0"
    },
    {
        color: "blue",
        value: "#00f"
    },
    {
        color: "cyan",
        value: "#0ff"
    }
]

I want add a property row_number to each element which contains the current index of the element in the array.

I need this because I am changing the order of the elements later on but want to be able to identify the original position of an element.

like image 285
Ali Avatar asked Sep 16 '15 13:09

Ali


People also ask

How do you add an index to an array?

You want to explicitly add it at a particular place of the array. That place is called the index. Array indexes start from 0 , so if you want to add the item first, you'll use index 0 , in the second place the index is 1 , and so on. To perform this operation you will use the splice() method of an array.

How do you add properties to an array?

Arrays are objects and therefore you can add your own properties to them: var arr = [1, 2, 3]; arr.

Can I use hasOwnProperty with an array?

The hasOwnProperty() method returns true if the property is directly present in the object (not in its prototype chain). If an object is an Array, then the hasOwnProperty() method can check if an index is available (not empty) in the array.

Can you use indexOf for an array of objects?

The Array. indexOf() method returns the index of the first matching item in an array (or -1 if it doesn't exist).


3 Answers

There is nothing really special to do. Simply iterate over the array and add the property to each element:

for (var i = 0; i < foo.length; i++) {
  foo[i].row_number = i;
}

// or with forEach

foo.forEach(function(row, index) {
  row.row_number = index;
});

See Access / process (nested) objects, arrays or JSON for more general information about nested data structures in JavaScript.

like image 177
Felix Kling Avatar answered Oct 08 '22 00:10

Felix Kling


You could also use map in case you want to keep the old object around, or wanted to do it in a call chain not modifying an existing array.

const oldArray = [{a: 'Andy'}, {b: 'Betty'}, {c: 'Charlie'}];
const newArray = oldArray.map((item, index) => ({index, ...item}));
console.log(newArray);
// Array [Object { index: 0, a: "Andy" }, Object { index: 1, b: "Betty" }, Object { index: 2, c: "Charlie" }]
like image 32
darkhipo Avatar answered Oct 08 '22 01:10

darkhipo


If foo is one object you can do

foo.Row_Number

if is list you can use $.each and do the same for each object.

$.each(obj, function (index) {
    this.row_number = index + 1;
});
like image 35
Arash R Avatar answered Oct 08 '22 01:10

Arash R