2

I have a JSON

const myJSON = [
    {
        name: 'compass',
        LocX: 35,
        LocY: 312
    },
    {
        name: 'another',
        LocX: 52,
        LocY: 32
    }
]

From the UI, if a user wants to add number 5 to one of the two properties, how should I handle this?

I am already doing

myJSON.map(x => x[userSelectedProperty] + 5)
// when user selected property is 'LocX', [40, 57]

but I want the full array with just the value updated.

[
    {
        name: 'compass',
        LocX: 40,
        LocY: 312
    },
    {
        name: 'another',
        LocX: 57,
        LocY: 32
    }
]

How do I do this?

5 Answers 5

4

Simply iterate and update the object property using Array#forEcah method which is enough for this purpose.

const myJSON = [{
    name: 'compass',
    LocX: 35,
    LocY: 312
  },
  {
    name: 'another',
    LocX: 52,
    LocY: 32
  }
];

let userSelectedProperty = 'LocX';
myJSON.forEach(x => x[userSelectedProperty] += 5);

console.log(myJSON);


In case you want to create a new array then use Array#map method.

const myJSON = [{
    name: 'compass',
    LocX: 35,
    LocY: 312
  },
  {
    name: 'another',
    LocX: 52,
    LocY: 32
  }
];

let userSelectedProperty = 'LocX';
let res = myJSON.map(x => {
  // copy properties to a new object
  let y = Object.assign({}, x);
  y[userSelectedProperty] += 5;
  return y;
});

console.log(res);

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

Comments

2

You could just use a .forEach loop on the array to update the property within it, rather than creating a new array.

const myJSON = [
    {
        name: 'compass',
        LocX: 35,
        LocY: 312
    },
    {
        name: 'another',
        LocX: 52,
        LocY: 32
    }
];
var userSelectedProperty = "LocX";
// Update the array.
myJSON.forEach(t => t[userSelectedProperty] += 5);

console.log(myJSON);

2 Comments

this mutates myJSON - not saying that's wrong in any way, it's unclear from the question
@JaromandaX Yup, I noticed the const there, and I was going to do something like you did in your answer. But since they didn't specify in question about this, using a forEach seems a better approach.
1

No need of map.Use forEach and update the key value

const myJSON = [{
    name: 'compass',
    LocX: 35,
    LocY: 312
  },
  {
    name: 'another',
    LocX: 52,
    LocY: 32
  }
]

function addNum(key, val) {
  myJSON.forEach(function(item) {
    item[key] = item[key] + val;

  })
}
addNum('LocX', 5)
console.log(myJSON)

1 Comment

this mutates myJSON - not saying that's wrong in any way, it's unclear from the question
1

Using Object.assign makes this simply

const myJSON = [
    {
        name: 'compass',
        LocX: 35,
        LocY: 312
    },
    {
        name: 'another',
        LocX: 52,
        LocY: 32
    }
]
let userSelectedProperty = 'LocX';
let newObject = myJSON.map(x => Object.assign({}, x, {[userSelectedProperty]: x[userSelectedProperty] + 5}));
console.log(newObject);

Comments

1

Try the following:

const myJSON = [
    {
        name: 'compass',
        LocX: 35,
        LocY: 312
    },
    {
        name: 'another',
        LocX: 52,
        LocY: 32
    }
]
var userInput = 5;
var final = myJSON.map((x, i)=>{
	return  {name:x.name ,LocX:x.LocX+userInput, LocY:x.LocY};
});
console.log(final);

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.