Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更改对象的数组_Javascript_Arrays_Reactjs_Redux - Fatal编程技术网

Javascript 更改对象的数组

Javascript 更改对象的数组,javascript,arrays,reactjs,redux,Javascript,Arrays,Reactjs,Redux,我正在变异产品数组,在这个数组中,我变异容器数组中一个元素的对象,比如产品[0]。我正在为产品的第0个元素的容器对象指定一个新对象。代码如下所示。但它不是将容器对象更新为给定的新对象,而是将其设置为数组 请参见产品数组索引0处的元素。这里是我将容器对象设置为新对象的地方。但在这里,它将数组设置为容器对象。请参阅下面的代码。还可以参见索引1处元素的容器对象。在索引1,这是正确的 (10) 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;


    }
  }

};
export const updateSelectedProduct=selectedContainer=>(dispatch,getState)=>{
让{selectedProduct}=getState().product;
let{products}=getState().product;
对于(var productList=0;productList
看起来您的第一个元素是数组类型:

container: (2) [{…}, {…}]
第二个元素是对象:

container: {user: null, id: 22, components: Array(1), currency: "EUR", total_price: 0.15, …}
这就是它将其设置为数组的原因

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

const产品=[
{
容器:[{id:1,货币:'USD'},{test:'Hello,World:')},
身份证号码:15,
加价系数:“1.30”,
名称:“Sohil测试产品”,
二等兵:错,
利润系数:“3.00”,
配方:{id:1,用户:空,成分:[],单位:“克”,数量:4,},
销售税:“12.00”,
标记:[{}],
总价:17.47,
用户:3
},
{
容器:{id:2,货币:'另一种货币'},
身份证号码:16,
加价系数:“4.00”,
名称:“演示产品24”,
二等兵:错,
利润系数:“4.00”,
配方:{id:38,用户:3,成分:[],单位:“克”,数量:0,},
销售税:“0.00”,
标签:[],
总价:“9.12”,
用户:3,,
},
{id:21,用户:3,名称:“演示产品”,标签:[],销售税:“2.00”,},
{id:22,用户:3,名称:“产品2”,标签:[],销售税:“4.00”,},
{id:23,用户:3,名称:“新产品即将上市”,标签:[],销售税:“3.00”,},
{id:24,用户:3,名称:“新产品”,标签:[],销售税:“0.00”,},
];
products.forEach((el,索引)=>{
让container=null;
让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={
组件:容器,
货币:container.currency,
id:container.id,
名称:空,
用户:空
};
}else if(products[index].container&&products[index].container.currency){
containerObj={
组成部分:el,
货币:产品[索引].container.currency,
id:products[index].container.id,
名称:空,
用户:空
};
}
产品[索引].container=containerObj;
});

console.log(products)
看起来您的第一个元素是数组类型:

container: (2) [{…}, {…}]
第二个元素是对象:

container: {user: null, id: 22, components: Array(1), currency: "EUR", total_price: 0.15, …}
这就是它将其设置为数组的原因

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

const产品=[
{
容器:[{id:1,货币:'USD'},{test:'Hello,World:')},
身份证号码:15,
加价系数:“1.30”,
名称:“Sohil测试产品”,
二等兵:错,
利润系数:“3.00”,
配方:{id:1,用户:空,成分:[],单位:“克”,数量:4,},
销售税:“12.00”,
标记:[{}],
总价:17.47,
用户:3
},
{
容器:{id:2,货币:'另一种货币'},
身份证号码:16,
加价系数:“4.00”,
名称:“演示产品24”,
二等兵:错,
利润系数:“4.00”,
配方:{id:38,用户:3,成分:[],单位:“克”,数量:0,},
销售税:“0.00”,
标签:[],
总价:“9.12”,
用户:3,,
},
{id:21,用户:3,名称:“演示产品”,标签:[],销售税:“2.00”,},
{id:22,用户:3,名称:“产品2”,标签:[],销售税:“4.00”,},
{id:23,用户:3,名称:“新产品即将上市”,标签:[],销售税:“3.00”,},
{id:24,用户:3,名称:“新产品”,标签:[],销售税:“0.00”,},
];
products.forEach((el,索引)=>{
让container=null;
让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={
组件:容器,
货币:container.currency,
id:container.id,
名称:空,
用户:空
};
}else if(products[index].container&&products[index].container.currency){
containerObj={
组成部分:el,
货币:产品[索引].container.currency,
id:products[index].container.id,
名称:空,
用户:空
};
}
产品[索引].container=containerObj;
});

console.log(products)
containerObj中包含的对象不会神奇地转换为两个对象的数组。请添加一个显示实际问题的数组。
containerObj
中包含的对象不会神奇地转换为两个对象的数组。请添加显示实际问题的。