gpt4 book ai didi

javascript - 改变对象的数组

转载 作者:行者123 更新时间:2023-11-28 03:24:27 24 4
gpt4 key购买 nike

我正在改变 products 数组,并且在其中,我正在改变数组中一个元素的 container 对象,比方说 products[0]>。我正在将一个新对象分配给产品第 0 个元素的容器对象。代码如下。但它不是将容器对象更新为给定的新对象,而是将其设置为数组。

查看产品数组索引 0 处的元素。这是我将 container 对象设置为新对象的位置。但这里它设置了一个数组到容器对象。请参阅下面的代码。还可以查看索引 1 处元素的容器对象。在索引 1 处,它是正确的。

(10) products = [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: container: (2) [{…}, {…}]
id: 15
markup_factor: "1.30"
name: "Sohil Test Product"
private: false
profit_factor: "3.00"
recipe: {id: 1, user: null, ingredients: Array(2), unit: "g", quantity: 4, …}
sales_tax: "12.00"
tags: [{…}]
total_price: 17.47
user: 3
__proto__: Object

1: container: {user: null, id: 22, components: Array(1), currency: "EUR", total_price: 0.15, …}
id: 16
markup_factor: "4.00"
name: "demo product 24"
private: false
profit_factor: "4.00"
recipe: {id: 38, user: 3, ingredients: Array(1), unit: "g", quantity: 0, …}
sales_tax: "0.00"
tags: []
total_price: "9.12"
user: 3
__proto__: Object
2: {id: 21, user: 3, name: "DEMO PRODUCT", tags: Array(1), sales_tax: "2.00", …}
3: {id: 22, user: 3, name: "product 2", tags: Array(0), sales_tax: "4.00", …}
4: {id: 23, user: 3, name: "new product coming", tags: Array(1), sales_tax: "3.00", …}
5: {id: 24, user: 3, name: "new product", tags: Array(1), sales_tax: "0.00", …}
6: {id: 25, user: 3, name: "new product", tags: Array(2), sales_tax: "0.00", …}
7: {id: 26, user: 3, name: "sample test", tags: Array(0), sales_tax: "0.00", …}
8: {id: 27, user: 3, name: "new product demo", tags: Array(0), sales_tax: "0.00", …}
9: {id: 28, user: 3, name: "new product 3", tags: Array(0), sales_tax: "1.00", …}
length: 10
__proto__: Array(0)

突变的代码是:

export const updateSelectedProduct = selectedContainer => (dispatch, getState) => {

let { selectedProduct } = getState().product;
let { products } = getState().product;
for (var productList = 0; productList < products.length; productList++) {
if (products[productList].id === selectedProduct.id) {
const containerObj = {
components: selectedContainer,
currency: products[productList].container.currency,
id: products[productList].container.id,
name: null,
user: null

};


products[productList].container = containerObj;


}
}

};

最佳答案

看起来你的第一个元素是数组类型:

container: (2) [{…}, {…}]

第二个元素是对象:

container: {user: null, id: 22, components: Array(1), currency: "EUR", total_price: 0.15, …}

所以这就是它将其设置为数组的原因。

您可以做的是获取第一个元素的对象,而不是数组,然后您的代码将起作用。让我举个例子:

const products = [
{
container : [{id: 1, currency: 'USD'}, {test: 'Hello, World:)'}],
id: 15,
markup_factor: "1.30",
name: "Sohil Test Product",
private: false,
profit_factor: "3.00",
recipe: {id: 1, user: null, ingredients: [], unit: "g", quantity: 4, },
sales_tax: "12.00",
tags: [{}],
total_price: 17.47,
user: 3
},
{
container : {id: 2, currency: 'Another Currency'},
id: 16,
markup_factor: "4.00",
name: "demo product 24",
private: false,
profit_factor: "4.00",
recipe: {id: 38, user: 3, ingredients: [], unit: "g", quantity: 0, },
sales_tax: "0.00",
tags: [],
total_price: "9.12",
user: 3,
},
{id: 21, user: 3, name: "DEMO PRODUCT", tags: [], sales_tax: "2.00", },
{id: 22, user: 3, name: "product 2", tags: [], sales_tax: "4.00", },
{id: 23, user: 3, name: "new product coming", tags: [], sales_tax: "3.00", },
{id: 24, user: 3, name: "new product", tags: [], sales_tax: "0.00", },
];


products.forEach((el, index) => {
let container = null;
let containerObj = null;
if (Array.isArray(el.container)
&& el.container.some(c=> c.currency && c.id)) {
container = el.container.find(c => c.currency && c.id);
containerObj = {
components: container,
currency: container.currency,
id: container.id,
name: null,
user: null
};
} else if(products[index].container && products[index].container.currency) {
containerObj = {
components: el,
currency: products[index].container.currency,
id: products[index].container.id,
name: null,
user: null
};
}
products[index].container = containerObj;
});
console.log(products)

关于javascript - 改变对象的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58778513/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com