我要变异 products 数组,就是要变异数组的一个元素的 container 对象,比方说 products [0] >。我正在为产品的第0个元素的容器对象分配一个新对象。代码如下。但是,不是将容器对象更新为给定的新对象,而是将其设置为数组。
请参阅产品数组下标 0 的元素。这是我将容器对象设置为新对象的位置。但是这里它为容器对象设置了一个数组。请参见下面的代码。另外,请参见索引为 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;
}
}
};
答案 0 :(得分:0)
看起来您的第一个元素是数组类型:
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)