我如何创建一个函数来更改位于数组内部的对象内的 属性,在 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
所以我目前已经在 JavaScript 任务上停留了几个小时。
所以我得到了一个包含以下数据的 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 }
]
}
我的作业说明如下:
Instruction
因此,根据我的理解,我需要创建一个包含两个字符串参数的函数,并且我需要将位于数组中的对象内部的 属性 更改为新的 属性.
到目前为止,这是我所做的:
'use strict'
const iceCream = require('./main.json')
let namespace = {
changeProp : function (newprop,oldprop) {
for (let oldprop in iceCream.info) {
}
}
}
我知道一点也不多,但我只是想要一些帮助,以了解我如何才能在我的作业中取得更多进展。
感谢任何帮助,谢谢
您可以使用 map, Object.keys, and reduce
轻松地将 oldKey
替换为 newKey
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; }
您可以这样做。简单、简单且令人讨厌的解决方案。
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"));
所以我目前已经在 JavaScript 任务上停留了几个小时。
所以我得到了一个包含以下数据的 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 }
]
}
我的作业说明如下:
Instruction
因此,根据我的理解,我需要创建一个包含两个字符串参数的函数,并且我需要将位于数组中的对象内部的 属性 更改为新的 属性.
到目前为止,这是我所做的:
'use strict'
const iceCream = require('./main.json')
let namespace = {
changeProp : function (newprop,oldprop) {
for (let oldprop in iceCream.info) {
}
}
}
我知道一点也不多,但我只是想要一些帮助,以了解我如何才能在我的作业中取得更多进展。
感谢任何帮助,谢谢
您可以使用 map, Object.keys, and reduce
轻松地将oldKey
替换为 newKey
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; }
您可以这样做。简单、简单且令人讨厌的解决方案。
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"));