0

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

4 Answers 4

3

Use map

array = array.map( s => Object.assign( s, {extrakey : "value" } ) );
Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

0

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);

Comments

0
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" } ) );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.