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
2 Answers
Reset to default 6You 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"}))