Javascript 更改JSON的值

Javascript 更改JSON的值,javascript,json,Javascript,Json,我想通过向所有值添加不同的和来更改移动的值 { "tile-type":"hex", "atlas":"hex-green", "moves":[61,73,87,103] } 因此,作为一个例子,如果我们分别向每个值添加1、2、3、4,最终结果必须是: "moves":[62,75,90,107]} 没有JSON表这样的东西。JSON是可以解析为对象的字符串。一旦解析了它,您就可以将其作为任何其他文件使用 var myJsonObject = JSON.parse(

我想通过向所有值添加不同的和来更改移动的值

{
    "tile-type":"hex",
    "atlas":"hex-green",
    "moves":[61,73,87,103]
}
因此,作为一个例子,如果我们分别向每个值添加1、2、3、4,最终结果必须是:

"moves":[62,75,90,107]}

没有JSON表这样的东西。JSON是可以解析为对象的字符串。一旦解析了它,您就可以将其作为任何其他文件使用

var myJsonObject = JSON.parse(yourJsonString);
myJsonObject.moves = myJsonObject.moves.map(i => i + 10)

只需在movie的object属性上进行迭代,并将另一个数组中的相应值相加即可

常量数据={tile-type:hex,atlas:hex-green,moves:[61,73,87103]}; 常数各自的_值=[1,2,3,4]; data.moves=data.moves.mapi,k=>i+各自的_值[k]
console.logdata这是一个常规javascript对象,您可以这样做:

const foo = {
 "tile-type": "hex",
 "atlas": "hex-green",
 "moves":[61,73,87,103]
}

foo.moves = foo.moves.map(el => el + 10)
const json = "{\"tile-type\":\"hex\",\"atlas\":\"hex-green\",\"moves\":[61,73,87,103]}"
const foo = JSON.parse(json)
foo.moves = foo.moves.map(el => el + 10)

// optionally, convert it back to JSON
const newJson = JSON.stringify(foo)
如果是JSON,它将如下所示:

const foo = {
 "tile-type": "hex",
 "atlas": "hex-green",
 "moves":[61,73,87,103]
}

foo.moves = foo.moves.map(el => el + 10)
const json = "{\"tile-type\":\"hex\",\"atlas\":\"hex-green\",\"moves\":[61,73,87,103]}"
const foo = JSON.parse(json)
foo.moves = foo.moves.map(el => el + 10)

// optionally, convert it back to JSON
const newJson = JSON.stringify(foo)

因此,请引用它并更新值。。。。。