javascript - 我可以更改 javascript 数组项目中的属性 (JSON.stringify) 吗?

标签 javascript json

我想更改 js 数组中项目的属性。

var settings = {
        "url": "yeahyeah",
        "method": "POST",
        "headers": {
            "Content-Type": "application/json",
        },
        "data":  JSON.stringify({
            des: foo,
            order:"ohYeah"
        })
    }

我尝试过这个:

if(something){
        settings.data.order = "ohNo";
}

将 js array () 中项目 settings.data 的属性 order 从“ohYeah”更改为“ohNo”。

最佳答案

这应该有效(使用 ternary operator )

var settings = {
        "url": "yeahyeah",
        "method": "POST",
        "headers": {
            "Content-Type": "application/json",
        },
        "data":  JSON.stringify({
            des: foo,
            order: something ? "ohNo" : "ohYeah"
        })
    }

关于javascript - 我可以更改 javascript 数组项目中的属性 (JSON.stringify) 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58797156/

相关文章:

sql - 使用 JSON 参数的 Postgres 批量插入函数

android - 使用 gson 解析 JSON 对象

php - 从 MySQL 编码 Json

php - 通过Jquery AJAX发送两条数据?

javascript - 使用 'for' 循环合并两个排序数组...如何在循环结束时阻止 'i' 增加

javascript - 我如何创建一个函数来更改位于数组内的对象内的属性,在 JSON 中

python - 无法从 http 请求中提取 JSON

asp.net-mvc - 如何将表单中的复选框序列化为Json数据

JavaScript | Angular | Controller 作为语法 : Cannot Use `this`

javascript - 无法将数据从 json 文件传递​​到 Chart.js 中的标签和数据集