最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - change value of specific keys of an object - Stack Overflow

programmeradmin0浏览0评论

lets assume i have the following object and I need to format only the keys of a and b and multiple them by 2. how can I do this? should I make a copy of the object? i have 8 keys that need to be changed so please try to find a short and efficient solution

const ob = {
a:1,
b:2,
c:3,
d:4
}

lets assume i have the following object and I need to format only the keys of a and b and multiple them by 2. how can I do this? should I make a copy of the object? i have 8 keys that need to be changed so please try to find a short and efficient solution

const ob = {
a:1,
b:2,
c:3,
d:4
}
Share Improve this question edited Sep 6, 2021 at 6:30 cgvfgf34 asked Sep 6, 2021 at 6:25 cgvfgf34cgvfgf34 391 gold badge1 silver badge6 bronze badges 4
  • 1 const newObject = {...ob, a: ob.a *2, b: ob.b * 2} – MorKadosh Commented Sep 6, 2021 at 6:27
  • is there a better way? i have 8 keys that need to be changed – cgvfgf34 Commented Sep 6, 2021 at 6:28
  • 1 "better way" he said, I dare you to calculate the bigO notation of that expression. Code won't write down by itself. With all respect sir. I remend you to read the how to ask a question. Blog of stack overflow – Ernesto Commented Sep 6, 2021 at 6:42
  • 1 @Ernesto Calm down man. By "better way" OP means easier to write, consider he has to change multiple properties, not just two. It is impractical to write ` * 2` a hundred times if there are a hundred fields to change. OP is not saying about the performance. – Ricky Mo Commented Sep 6, 2021 at 7:01
Add a ment  | 

6 Answers 6

Reset to default 1

Provided that you need to changes many keys. You may consider this way

const ob = {
  a:1,
  b:2,
  c:3,
  d:4,
  e:5,
  f:6,
  g:7,
  h:8,
  i:9,
  j:10,
  k:11,
  l:12,
  m:13,
  n:14,
  o:15
}

const keysToChange = ["a","b","c","d","e","f","g","h"];
for(let key of keysToChange)
{
  ob[key] = ob[key] * 2;
}
console.log(ob);

If you want the original object and the new object then proceed as follow

const ob = {
  a: 1,
  b: 2,
  c: 3,
  d: 4
};

console.log(ob);

let new_obj = {};           
Object.assign(new_obj, ob);//copy the original object to the new object
//change the value of new keys one by one
new_obj['a'] = new_obj['a'] * 2;
new_obj['b'] = new_obj['b'] * 2;

console.log(new_obj);

//alternatively if you want to loop through all keys and change values for keys 'a' and 'b', proceed as follows:

for(key in new_obj) {
    if(key=='a' || key =='b'){
        new_obj[key] = new_obj[key] * 2;
    }    
}

console.log(new_obj);

A more optimized or shorter way in case if you have more keys, would be:

const ob = {
  a: 1,
  b: 2,
  c: 3,
  d: 4
};
const keysToChange = ['a', 'b'];

console.log(ob);

keysToChange.forEach(key => {
  if (ob[key]) {
    ob[key] = ob[key] * 2;
  }
});

console.log(ob);

You can do it manually like that

const ob = {
  a: 1,
  b: 2,
  c: 3,
  d: 4
};
console.log(ob);
ob['a'] = ob['a'] * 2;
ob['b'] = ob['b'] * 2;
console.log(ob);

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Objects</h2>

<p id="demo"></p>

<script>
// Create an object:
const ob = {
  a:1,
b:2,
c:3,
d:4
};

ob.a=ob.a*2;
ob.b=ob.b*2;


document.getElementById("demo").innerHTML =
ob.a + "  " + ob.b ;
</script>

</body>
</html>

If you already knows keys which you must update you can store them in an array and loop through that array and for each key update the related key in the object.

const ob = {
  a:1,
  b:2,
  c:3,
  d:4
};

let updatedKeys = ['a', 'b'];

updatedKeys.forEach(key => {
  if(Object.keys(ob).indexOf(key) !== -1) {
      ob[key] *= 2;
  }
});

console.log(ob);

发布评论

评论列表(0)

  1. 暂无评论