Ankit
Ankit

Reputation: 1011

Add Extra Key value on existing object under loop

I have a data set like this:

[
        {
            "_id": "5aa7b6add9655d0bd4ce1f53",
            "user_name": "as",
            "createdate": "2018-03-13T11:31:57.133Z",
        },
        {
            "_id": "5aa7b6add9655d0bd4ce1f54",
            "user_name": "ds",
            "createdate": "2018-03-13T11:31:57.133Z",
        },
]

Now For getting the value one by one I have created a loop. Under loop I am getting the value.

Now if I want to add extra key value on that object then how it will be done.?

I tried data[i].extrakey = "value"; console.log(data); but it will not set.

Any help is really appreciated

Upvotes: 0

Views: 646

Answers (4)

Talib Allauddin
Talib Allauddin

Reputation: 109

var array = [
    {
        "_id": "5aa7b6add9655d0bd4ce1f53",
        "user_name": "as",
        "createdate": "2018-03-13T11:31:57.133Z",
    },
    {
        "_id": "5aa7b6add9655d0bd4ce1f54",
        "user_name": "ds",
        "createdate": "2018-03-13T11:31:57.133Z",
    }
];
array = array.map( s => Object.assign( s, {extrakey : "value" } ) );

Upvotes: 0

Suren Srapyan
Suren Srapyan

Reputation: 68685

You can see it in the example below. Just iterating over the array and adding a new key will alter the objects inside the array, because object is a reference type and you are just working with a reference which changes the original object.

const array = [
        {
            "_id": "5aa7b6add9655d0bd4ce1f53",
            "user_name": "as",
            "createdate": "2018-03-13T11:31:57.133Z",
        },
        {
            "_id": "5aa7b6add9655d0bd4ce1f54",
            "user_name": "ds",
            "createdate": "2018-03-13T11:31:57.133Z",
        }
];

array.forEach(item => item.extraKey = 'value');

console.log(array);

Also if you want to change the array items, it will be better to not change the original objects, but create their copies and provide a new array.

const array = [
        {
            "_id": "5aa7b6add9655d0bd4ce1f53",
            "user_name": "as",
            "createdate": "2018-03-13T11:31:57.133Z",
        },
        {
            "_id": "5aa7b6add9655d0bd4ce1f54",
            "user_name": "ds",
            "createdate": "2018-03-13T11:31:57.133Z",
        }
];

const newArray = array.map(item => Object.assign({}, item, { extraKey: 'value' }));

console.log(newArray);

Upvotes: 0

Faly
Faly

Reputation: 13356

You can use spread along with array.prototype.map to add the extra key:

var arr = [
        {
            "_id": "5aa7b6add9655d0bd4ce1f53",
            "user_name": "as",
            "createdate": "2018-03-13T11:31:57.133Z",
        },
        {
            "_id": "5aa7b6add9655d0bd4ce1f54",
            "user_name": "ds",
            "createdate": "2018-03-13T11:31:57.133Z",
        },
];

var result = arr.map(e => ({...e, extraKey: 'value'}));

console.log(result);

Upvotes: 0

gurvinder372
gurvinder372

Reputation: 68443

Use map

array = array.map( s => Object.assign( s, {extrakey : "value" } ) );

Upvotes: 3

Related Questions