admin管理员组文章数量:1302328
I've searched for a long time, but cannot find any solution for my problem... I got a random nested json object like this:
var obj = { a: 1, b: 2, c: { a: 1, b: 2, c: {a: 'hello', b: 'HowAreYou?'} } };
someValue = 'hello';
newValue = 'bye';
Now I'm going to search for 'someValue'. If 'obj' contains this value the alert will be triggered. Here's my example:
function wannaChangeValue(obj) {
var itemKey;
if (obj instanceof Object) {
for (itemKey in obj) {
if (obj.hasOwnProperty(itemKey)) {
wannaChangeValue(obj[itemKey]);
}
}
} else {
alert(obj);
if (obj == someValue) {
alert('got it! now change...')
//change someValue to newValue
}
}
return obj
}
wannaChangeValue(obj)
This works really fine, but how can I change 'someValue' to 'newValue' and return the whole json-file again? I've seen many examples how to handle this, but I need a solution for ANY kind of nested json object WITHOUT knowing the path for changing this value. Maybe this is a pletely wrong approach... Thanks in advance!
I've searched for a long time, but cannot find any solution for my problem... I got a random nested json object like this:
var obj = { a: 1, b: 2, c: { a: 1, b: 2, c: {a: 'hello', b: 'HowAreYou?'} } };
someValue = 'hello';
newValue = 'bye';
Now I'm going to search for 'someValue'. If 'obj' contains this value the alert will be triggered. Here's my example:
function wannaChangeValue(obj) {
var itemKey;
if (obj instanceof Object) {
for (itemKey in obj) {
if (obj.hasOwnProperty(itemKey)) {
wannaChangeValue(obj[itemKey]);
}
}
} else {
alert(obj);
if (obj == someValue) {
alert('got it! now change...')
//change someValue to newValue
}
}
return obj
}
wannaChangeValue(obj)
This works really fine, but how can I change 'someValue' to 'newValue' and return the whole json-file again? I've seen many examples how to handle this, but I need a solution for ANY kind of nested json object WITHOUT knowing the path for changing this value. Maybe this is a pletely wrong approach... Thanks in advance!
Share Improve this question asked Aug 3, 2017 at 9:46 EndivieEndivie 1011 gold badge2 silver badges8 bronze badges 1- Possible duplicate of Find and update in nested json object. Another link that might help: stackoverflow./questions/15523514/… – Rajesh Commented Aug 3, 2017 at 9:58
6 Answers
Reset to default 4You could use a recursice approach for any found object, call the function again.
function update(object, search, replace) {
Object.keys(object).forEach(function (k) {
if (object[k] && typeof object[k] === 'object') {
return update(object[k], search, replace)
}
if (object[k] === search) {
object[k] = replace;
}
});
}
var object = { a: 1, b: 2, c: { a: 1, b: 2, c: {a: 'hello', b: 'HowAreYou?'} } };
update(object, 'hello', 'bye');
console.log(object)
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can create function using for...in
loop and if current value match oldValue change that value.
var obj = { a: 1, b: 2, c: { a: '1', b: 2, c: {a: 'hello', b: 'HowAreYou?'} } };
var someValue = 'hello';
var newValue = 'bye';
function changeVal(data, oldV, newV) {
for(var i in data) {
if(typeof data[i] == 'object') changeVal(data[i], oldV, newV);
if(data[i] == oldV) data[i] = newV
}
return data
}
console.log(changeVal(obj, someValue, newValue))
You can define a function which search recursively the value
through your object and then replace this value.
function replaceObj (obj, valueForSearch,valueForReplace) {
for (var key in obj) {
var value = obj[key];
if (typeof value === 'object') {
replaceObj(value, valueForSearch,valueForReplace);
}
if (value === valueForSearch) {
obj[key]=valueForReplace;
}
}
}
let obj = { a: 1, b: 2, c: { a: 1, b: 2, c: {a: 'hello', b: 'HowAreYou?'} } };
let someValue = 'hello';
let newValue = 'bye';
replaceObj(obj,someValue,newValue);
console.log(obj);
This is a quick solution:
function wannaChangeValue(obj) {
var itemKey;
if (obj instanceof Object) {
for (itemKey in obj) {
if (obj.hasOwnProperty(itemKey)) {
obj[itemKey] = wannaChangeValue(obj[itemKey]);
}
}
} else {
alert(obj);
if (obj == someValue) {
alert('got it! now change...')
//change someValue to newValue
obj = someValue;
}
}
return obj
}
wannaChangeValue(obj)
Obviously this will mutate the value on the object itself, so if you want an entirely new object you will have to do something different.
You could use recursive call for your object.
Demo
var object = { a: 1, b: 2, c: { a: '1', b: 2, c: {a: 'hello', b: 'HowAreYou?'} } },
someValue = 'hello',
newValue = 'bye';
function wannaChangeValue(obj) {
var itemKey;
if (obj instanceof Object) {
for (itemKey in obj) {
if (obj.hasOwnProperty(itemKey)) {
if (!(obj[itemKey] instanceof Object) && obj[itemKey] == someValue) {
//alert(someValue + ' got it! now change...')
//change someValue to newValue
obj[itemKey] = newValue;
break;
} else {
wannaChangeValue(obj[itemKey]);
}
}
}
}
}
wannaChangeValue(object);
console.log(object)
.as-console-wrapper {max-height: 100% !important;top: 0;}
Here is a solution using object-scan
// const objectScan = require('object-scan');
const obj = { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 'hello', b: 'HowAreYou?' } } };
const replace = (input, oldValue, newValue) => objectScan(['**'], {
abort: true,
rtn: 'bool',
filterFn: ({ value, parent, property }) => {
if (value === oldValue) {
parent[property] = newValue;
return true;
}
return false;
}
})(input);
console.log(replace(obj, 'hello', 'bye')); // returns true iff replace happened
// => true
console.log(obj);
// => { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 'bye', b: 'HowAreYou?' } } }
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/[email protected]"></script>
Disclaimer: I'm the author of object-scan
本文标签: jqueryReplace value while iterating through nested json file in javascriptStack Overflow
版权声明:本文标题:jquery - Replace value while iterating through nested json file in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741687075a2392502.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论