简体   繁体   English

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

[英]How do i create a function that will change the property inside an object that is located inside of an array, in a JSON

so ive been currently stuck on a JavaScript assignment for hours now.所以我现在被一个 JavaScript 任务困住了几个小时。

so ive been given a Json file with the following data :所以我得到了一个包含以下数据的 Json 文件:

{
    "owner": "Jun31d",
    "info": [{ "id": 1, "name": "bob", "flavour": "vanilla", "age": 43 },
             { "id": 2, "name": "Juneid", "flavour": "Chocolate", "age": 19 },
             { "id": 3, "name": "Patrick", "flavour": "Strawberry", "age": 25 },
             { "id": 4, "name": "Jean", "flavour": "Mint", "age": 31 },
             { "id": 5, "name": "Ahmad", "flavour": "vanilla", "age": 18 },
             { "id": 6, "name": "Ali", "flavour": "Bubblegum", "age": 19 },
             { "id": 7, "name": "Frank", "flavour": "Pistachio", "age": 23 }
            ]
}

The instruction for my assigment is the following :我的分配说明如下:

Instruction操作说明

So from what ive understood i need to create a function that holds two string parameters, And ill need to change the property inside of my object that is located in an array to a new property.因此,根据我的理解,我需要创建一个包含两个字符串参数的函数,并且需要将位于数组中的对象内部的属性更改为新属性。

And until now heres what i did :直到现在继承人我所做的:

'use strict'

const iceCream = require('./main.json')

let namespace = {

    changeProp : function (newprop,oldprop) {
        for (let oldprop in iceCream.info) {
           
        }
    }


}

I know it is not much at all, but i just want some help to know how can i move forward a little bit more on my assigment.我知道这并不多,但我只是想要一些帮助,以了解我如何在我的分配上更进一步。

Any help is appreciated, thank you任何帮助表示赞赏,谢谢

You can easily replace the oldKey with newKey using map , Object.keys , and reduce您可以轻松地更换oldKeynewKey使用地图Object.keys ,并减少

 const data = { owner: "Jun31d", info: [ { id: 1, name: "bob", flavour: "vanilla", age: 43 }, { id: 2, name: "Juneid", flavour: "Chocolate", age: 19 }, { id: 3, name: "Patrick", flavour: "Strawberry", age: 25 }, { id: 4, name: "Jean", flavour: "Mint", age: 31 }, { id: 5, name: "Ahmad", flavour: "vanilla", age: 18 }, { id: 6, name: "Ali", flavour: "Bubblegum", age: 19 }, { id: 7, name: "Frank", flavour: "Pistachio", age: 23 }, ], }; function changePropertyName(arr, oldName, newName) { return arr.map((o) => { return Object.keys(o).reduce((acc, curr) => { curr === oldName ? (acc[newName] = o[oldName]) : (acc[curr] = o[curr]); return acc; }, {}); }); } console.log(changePropertyName(data.info, "flavour", "bestFlavour"));
 /* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */ .as-console-wrapper { max-height: 100% !important; top: 0; }

This is how you can do it.这就是你可以做到的。 Easy, simple and nasty solution.简单,简单和讨厌的解决方案。

 const data = { owner: 'Jun31d', info: [ { id: 1, name: 'bob', flavour: 'vanilla', age: 43 }, { id: 2, name: 'Juneid', flavour: 'Chocolate', age: 19 }, { id: 3, name: 'Patrick', flavour: 'Strawberry', age: 25 }, { id: 4, name: 'Jean', flavour: 'Mint', age: 31 }, { id: 5, name: 'Ahmad', flavour: 'vanilla', age: 18 }, { id: 6, name: 'Ali', flavour: 'Bubblegum', age: 19 }, { id: 7, name: 'Frank', flavour: 'Pistachio', age: 23 } ] }; const renameProperty = (a, e, n) => a.map((el) => { el[n] = el[e]; delete el[e]; return el; }); console.log(renameProperty(data.info, "id", "_id"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为位于数组内的值创建按钮,而同一数组位于另一个数组内? - how can I create buttons for values located inside an array, and that same array is located inside another array? 如何记录嵌套在数组内的数组中的对象属性? - How do I log an object property that is nested in an array inside an array? 如何在随机选择的对象内创建随机属性? - How do I create a random property inside a randomly selected object? 如何向位于数据阵列内的图像添加超链接? - How do I add a hyperlink to an image located inside a data array? 如何在反应钩子中创建新的 JSON object? - How do I create a new JSON object inside a react hook? 如何在JSON中的对象内部加载数组的元素? - How do I load the elements of an array inside of an object in JSON? 我如何在ejs中的函数内部访问数组对象 - How do i access an array object inside a function in ejs 如何更改json数组中json对象中放置在数组中的值? - How to change a value placed inside a array in a json object in json array? 在另一个数组中的一个数组中更改 object 的 object 属性 - Change object property of an object inside an array inside another array 更改自己对象中函数内部对象的属性 - Change property on object inside a function in the own object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM