如何使用查询风格在 JSON 中插入数据?

我们假设以下 json 文件


{


    "id": "test",

    "child" : [

                {    

                 "id": "alpha",

                 "child":[]

                },

                {    

                 "id": "beta",

                 "child":[]

                }


              ]


}

在 JavaScript 中,如何使用类似风味的查询将 JSON 对象插入特定的 JSON 数组位置


我想插入邻居 id 为 alpha 的孩子。


我不想使用位置


阿波罗的战车
浏览 129回答 2
2回答

莫回无

您可以编写一个递归函数来在 JSON 对象的指定索引处插入一个元素,如下所示:const yourJSON = `{&nbsp; &nbsp; "id": "test",&nbsp; &nbsp; "child" : [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"id": "alpha",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"child":[]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"id": "beta",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"child":[]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]}`;function insertIntoChildJSON(sourceJSON, childId, childPosition, value) {&nbsp; &nbsp; function insertIntoChild(data, childId, childPosition, value) {&nbsp; &nbsp; &nbsp; &nbsp; if (data.id === childId) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data.child[childPosition] = value;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (data.child.length > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (let i = 0; i < data.child.length; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data.child[i] = insertIntoChild(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data.child[i],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; childId,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; childPosition,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return data;&nbsp; &nbsp; }&nbsp; &nbsp; const parsedSource = JSON.parse(sourceJSON);&nbsp; &nbsp; return JSON.stringify(&nbsp; &nbsp; &nbsp; &nbsp; insertIntoChild(parsedSource, childId, childPosition, value)&nbsp; &nbsp; );}console.log(&nbsp; &nbsp; insertIntoChildJSON(yourJSON, 'alpha', 1, { id: 'charlie', child: [] }));

桃花长相依

假设file.json是您的 json 字符串import objects from './file.json'对于查询,你可以想出这样的东西。function query(column, value) {&nbsp; let children = objects.child&nbsp; return children.find(child => child[column] === value)}对于多个返回(数组),请filter改用function query(column, value) {&nbsp; let children = objects.child&nbsp; return children.filter(child => child[column] === value)}然后插入数据将是:function insert(column, value, insert) {&nbsp; if (insert) {&nbsp; &nbsp; let children = objects.child&nbsp; &nbsp; let item = children.find(child => child[column] === value)&nbsp; &nbsp; if (variable.constructor === Array) {&nbsp; &nbsp; &nbsp; item.child.push(...insert)&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; item.child.push(insert)&nbsp; &nbsp; }&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript