node.js - Append serial number property to array of objects in javascript -


i have array of objects in javascript.

var obj_arr =  [{     data_id: 281,     data_name: 'cim',     data_state: '0' }, {     data_id: 382,     data_name: 'cimx',     data_state: '0'  }, {     data_id: 482,     data_name: 'cimy',     data_state: '1'  }] 

i append serial number each of object in array. appended object this;

var obj_arr_appended =  [{     serial_no: 1,     data_id: 281,     data_name: 'cim',     data_state: '0' }, {     serial_no: 2,     data_id: 382,     data_name: 'cimx',     data_state: '0'  }, {     serial_no: 3,     data_id: 482,     data_name: 'cimy',     data_state: '1'  }] 

the serial number auto-increment. using node.js v6

with array.map can run function on each element:

var obj_arr_appended = obj_arr.map(function(currentvalue, index) {    currentvalue.serial_no = index    return currentvalue }) 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -