0

Im trying to serach the key named "SerialNumber" in array of objects and replace with "SNo"

var object = [{
    "SerialNumber": 001,
    "name": "user1"
}, {
    "SerialNumber": 002,
    "name": "user2"
}];
if ('SerialNumber' in object[0]) {
    object.forEach(function (key) {
        key.SNo = key.SerialNumber;
        delete key.SerialNumber;
    });
}
console.log(object)

if object = [], then the above code is not working. Is there any better approach (with good performance) to search the key in array of objects and replace with the new key.

2
  • 2
    Maybe do a check to see if the array is empty before you access object[0] As in if (object.length > 0) { ... } And maybe don't call the array object. Commented Aug 26, 2016 at 7:39
  • Note: your current code doesn't "search" - you test for a property of that name in the first element only, and if it exists you then process every element in the array whether they have a SerialNumber property or not. Commented Aug 26, 2016 at 7:42

6 Answers 6

4

You could use Array#forEach and check if the key exist.

function replace(oldKey, newKey, array) {
    array.forEach(function (o) {
        if (oldKey in o) {
            o[newKey] = o[oldKey];
            delete o[oldKey];
        }
    });
}

var object = [{ "SerialNumber": '001', "name": "user1" }, { "SerialNumber": '002', "name": "user2" }];
replace('SerialNumber', 'SNo', object);

console.log(object);

Sign up to request clarification or add additional context in comments.

Comments

2

It looks as if you switched if and foreach. object is an Array, so you can forEach through items, and there check for your condition.

var object = [
    {
        "SerialNumber": 001,
        "name": "user1"
    },
    {
        "SerialNumber": 002,
        "name": "user2"
    }
];

object.forEach(function (item)  {
    if (item.SerialNumber !== undefined) {
        item.SNo = item.SerialNumber;
        delete item.SerialNumber;
    }
});

Comments

1

Try something like this

object.forEach(function(obj){
  if(obj.hasOwnProperty("SerialNumber")){
    obj["SNo"] = obj["SerialNumber"];
    delete obj["SerialNumber"];
  }
});

4 Comments

Don't use for...in with arrays.
Thanks for addressing. May i know the reason please? @MaxArt
Thank you. I got the reason :) @MaxArt
Perfect. Now, if you edit your snippet accordingly I'll remove the -1.
0

Check for the key after getting the objects from array.

object.forEach(function (key) {
    if ('SerialNumber' in key) {
        key.SNo = key.SerialNumber;
        delete key.SerialNumber;
    }
});

It will work if the object has no element.

Comments

0

When you want to change the key of the object in a array then you better prefer mapping the array elements to itself.

Here is an example.

var object = [{
        "SerialNumber": 001,
        "name": "user1"
        }, {
        "SerialNumber": 002,
        "name": "user2"
       }];
    object = object.map(function(value) {
       var tempObj = value;
       if (tempObj.SerialNumber) {
         tempObj.SNo = tempObj.SerialNumber;
         delete tempObj.SerialNumber;
       }
       return tempObj;
    });

    console.log(object);

Comments

0

You have to move the if condition into the forEach callback. Otherwise it will only look for the SerialNumber of the first element and crash if it doesn't exists.

var array = [{
    "SerialNumber": 001,
    "name": "user1"
}, {
    "SerialNumber": 002,
    "name": "user2"
}];
array.forEach(function (element) {
    if(element.SerialNumber) {
        element.SNo = element.SerialNumber;
        delete element.SerialNumber;
    }
});
console.log(array);

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.