如何在 JavaScript 中的 Mongoose 响应数组中添加键值对
How to add key value pair in array of Mongoose response in JavaScript
我尝试做基本的事情。我有一个包含多个对象的数组。我想在每个数组对象中添加新的键值对。
我通过以下代码尝试了这个。
exports.addBuyOption = (arr) => {
var newArr=arr;
return new Promise((resolve, reject) => {
for (let i = 0; i < newArr.length; i++) {
newArr[i].name="new name" //updating the existing key value
newArr[i].canBuy=true //new key value
}
setTimeout(() => {
resolve(newArr);
}, 2000)
})
}
我添加了设置超时,因为我只是想确认循环操作后是否返回了承诺。此外,当代码不使用原始数组 运行 时,我会使用 newArr 名称创建一个新变量,但代码也不起作用。
exports.addBuyOption = (arr) => {
return new Promise((resolve, reject) => {
for (let i = 0; i < arr.length; i++) {
arr[i].name="new name" //updating the existing key value
arr[i].canBuy=true //new key value
}
resolve(arr);
})
}
使用此代码,我能够更新现有的键值,但无法添加任何新的键值。我究竟做错了什么?我尝试将添加键的方法从点运算符更改为数组索引到 Object.assign,但其中 none 对我有用。
With this code, I was able to update my existing key-value but was not able to add any new key value.
对此的唯一解释是数组中的对象已经应用了 Object.preventExtensions
。这是一个例子:
function addBuyOption(arr) {
for (let i = 0; i < arr.length; i++) {
arr[i].name="new name" //updating the existing key value
arr[i].canBuy=true //new key value
}
return arr;
}
const arr = [
{name: "old name 1"},
{name: "old name 2"},
{name: "old name 3"},
].map(Object.preventExtensions);
console.log("Before:");
console.log(JSON.stringify(arr, null, 4));
addBuyOption(arr);
console.log("After:");
console.log(JSON.stringify(arr, null, 4));
.as-console-wrapper {
max-height: 100% !important;
}
(注意我取消了承诺,它在这段代码中没有做任何有用的事情。)
如果是这种情况,则您无法向对象添加属性。但是您可以使用新的 属性:
创建一个包含对象副本的新数组
function addBuyOption(arr) {
return arr.map(obj => ({
...obj, // Copy existing properties
name: "new name", // Set new value for existing property
canBuy: true, // Set new property
}));
}
const arr = [
{id: 1, name: "old name 1"},
{id: 2, name: "old name 2"},
{id: 3, name: "old name 3"},
].map(Object.preventExtensions);
console.log("Before:");
console.log(JSON.stringify(arr, null, 4));
const newArr = addBuyOption(arr);
console.log("After:");
console.log(JSON.stringify(newArr, null, 4));
.as-console-wrapper {
max-height: 100% !important;
}
我尝试做基本的事情。我有一个包含多个对象的数组。我想在每个数组对象中添加新的键值对。
我通过以下代码尝试了这个。
exports.addBuyOption = (arr) => {
var newArr=arr;
return new Promise((resolve, reject) => {
for (let i = 0; i < newArr.length; i++) {
newArr[i].name="new name" //updating the existing key value
newArr[i].canBuy=true //new key value
}
setTimeout(() => {
resolve(newArr);
}, 2000)
})
}
我添加了设置超时,因为我只是想确认循环操作后是否返回了承诺。此外,当代码不使用原始数组 运行 时,我会使用 newArr 名称创建一个新变量,但代码也不起作用。
exports.addBuyOption = (arr) => {
return new Promise((resolve, reject) => {
for (let i = 0; i < arr.length; i++) {
arr[i].name="new name" //updating the existing key value
arr[i].canBuy=true //new key value
}
resolve(arr);
})
}
使用此代码,我能够更新现有的键值,但无法添加任何新的键值。我究竟做错了什么?我尝试将添加键的方法从点运算符更改为数组索引到 Object.assign,但其中 none 对我有用。
With this code, I was able to update my existing key-value but was not able to add any new key value.
对此的唯一解释是数组中的对象已经应用了 Object.preventExtensions
。这是一个例子:
function addBuyOption(arr) {
for (let i = 0; i < arr.length; i++) {
arr[i].name="new name" //updating the existing key value
arr[i].canBuy=true //new key value
}
return arr;
}
const arr = [
{name: "old name 1"},
{name: "old name 2"},
{name: "old name 3"},
].map(Object.preventExtensions);
console.log("Before:");
console.log(JSON.stringify(arr, null, 4));
addBuyOption(arr);
console.log("After:");
console.log(JSON.stringify(arr, null, 4));
.as-console-wrapper {
max-height: 100% !important;
}
(注意我取消了承诺,它在这段代码中没有做任何有用的事情。)
如果是这种情况,则您无法向对象添加属性。但是您可以使用新的 属性:
创建一个包含对象副本的新数组function addBuyOption(arr) {
return arr.map(obj => ({
...obj, // Copy existing properties
name: "new name", // Set new value for existing property
canBuy: true, // Set new property
}));
}
const arr = [
{id: 1, name: "old name 1"},
{id: 2, name: "old name 2"},
{id: 3, name: "old name 3"},
].map(Object.preventExtensions);
console.log("Before:");
console.log(JSON.stringify(arr, null, 4));
const newArr = addBuyOption(arr);
console.log("After:");
console.log(JSON.stringify(newArr, null, 4));
.as-console-wrapper {
max-height: 100% !important;
}