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

Replace all value in JSON object array with key in JavaScript - Stack Overflow

programmeradmin5浏览0评论

Any one give suggestion . Without looping the json object . Need to replace the value by key.

Example json

var Arr = [{'name':'rajn',  'age':20 , 'status': 'offline'},
{'name':'jhon',  'age':30 , 'status': 'offline'},
{'name':'antoon',  'age':50 , 'status': 'offline'},
{'name':'knhon',  'age':40 , 'status': 'offline'}]'

Now here in one shot have to replace all status to online ..

Is that any pre defined javascript function we can use ? How can we do this very simple steps . Normal iterate and change with key to value ..

Thank you

Any one give suggestion . Without looping the json object . Need to replace the value by key.

Example json

var Arr = [{'name':'rajn',  'age':20 , 'status': 'offline'},
{'name':'jhon',  'age':30 , 'status': 'offline'},
{'name':'antoon',  'age':50 , 'status': 'offline'},
{'name':'knhon',  'age':40 , 'status': 'offline'}]'

Now here in one shot have to replace all status to online ..

Is that any pre defined javascript function we can use ? How can we do this very simple steps . Normal iterate and change with key to value ..

Thank you

Share Improve this question edited Jul 25, 2018 at 7:52 Jack jdeoel 4,5846 gold badges28 silver badges56 bronze badges asked Jul 25, 2018 at 7:48 SubhaSubha 7612 gold badges9 silver badges24 bronze badges 2
  • 2 There's no such thing as "JSON object array". Also, there's no "one shot way" to do this, you will always need to iterate the array. – Teemu Commented Jul 25, 2018 at 7:50
  • You can't do this without looping, you can use Array.forEach() but it uses loop underneath – barbsan Commented Jul 25, 2018 at 7:54
Add a ment  | 

2 Answers 2

Reset to default 6

You can use Array.forEach() to modify the status to online.

var Arr = [{'name':'rajn', 'age':20 , 'status': 'offline'},{'name':'jhon', 'age':30 , 'status': 'offline'},{'name':'antoon', 'age':50 , 'status': 'offline'},{'name':'knhon', 'age':40 , 'status': 'offline'}];
Arr.forEach((item) => item.status = 'online');
console.log(Arr);

with spread operator es6, you can do this :

Arr.map(a => ({ ...a, status:"online"}))
发布评论

评论列表(0)

  1. 暂无评论