1

I am trying to convert all elements of a given key in a list of objects. The specific change is to convert an generic object to a Long object with the same values.

Input:

[obj1, obj2, obj3, ...]

Where each object looks like:

{
    key1: value1, // value1 is an object
    key2: value2
}

So the goal here is to get a very similar array, but where key1 is transformed from just object to a long object, in my example incremented by one.

Output:

[obj1, obj2, obj3]

Where each object is:

{
    key1: value1, // value1 is now a Long object
    key2: value2
}

I tried with mapping over the array and then spreading over the objects entries, but did not succeed.

Any help would be highly appreciated.

4
  • 1
    The code you tried really should be part of the question. Commented Mar 9, 2017 at 14:54
  • should be a very simple map. What did you try? Commented Mar 9, 2017 at 14:55
  • please add some real example with the wanted result, beside the code, you tried. Commented Mar 9, 2017 at 14:57
  • Thanks for the comments. I updated the question to be more specific. I also added an answer below with the final solution. Commented Mar 10, 2017 at 8:36

4 Answers 4

1

You don't really need to map here, unless you want to create one more new array. Simple loop would be enough, just update key1 or each object:

[obj1, obj2, obj3].forEach(obj => {
  obj.key1 = obj.key1 + 1
})
Sign up to request clarification or add additional context in comments.

Comments

0

Since the array holds object - any modification to object reflect the actual input being passed. You need to make a copy of your object and perform the logic.

Your logic,

var input = [obj1, obj2, obj3];
var output = input.map(function(obj){
    obj.key1 = obj.key1 + 1;
    return obj;
});

console.log(input[0]===output[0]); //true

Solution:

var output = input.map(function(obj){
    var _objCopy = Object.assign({}, obj);
    _objCopy.key1 = _objCopy.key1 + 1;
    return _objCopy;
});

console.log(input[0]===output[0]); //false

Comments

0

how about dynamically first key of any object es6 way

 const myObject0 = {
           'foo1': { name: 'myNam1' },
           'foo2': { name: 'myNam2' }
        };

        const myObject1 = {
           'yo': { name: 'myNam1' },
           'boh': { name: 'myNam2' }
        };


        [myObject0, myObject1].forEach(obj => {
            let getFirstKey = Object.keys(obj)[0];
            getFirstKey = getFirstKey + 1;
            console.log(getFirstKey);

        });

Comments

0

Thanks for all the answers guys.

I ended up with the following:

Given the input

[
    {
        key1: value1, // value1 is an object
        key2: value2
    },

    {
        key1: value1, // value1 is also an object
        key2: value2
    }
]

Solution: I ended up with,

  const testvar = [obj1, obj2, obj3].map((obj) => {
    return {
      ...obj,
      key1: new Long.fromValue(obj.key1)
    }
  });

  console.log(testvar);

Which gives the output

[
    {
        key1: value1, // value1 is now a Long object
        key2: value2
    },

    {
        key1: value1, // value1 is also now a Long object
        key2: value2
    }
]

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.