【问题标题】:Changing array of the object改变对象的数组
【发布时间】:2020-03-05 19:00:24
【问题描述】:

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

查看产品数组索引 0 处的元素。这是我将 container 对象设置为新对象的地方。但是这里它设置了一个数组到 container 对象。请参阅下面的代码。还可以查看索引 1 处元素的 container 对象。在索引 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;


    }
  }

};

【问题讨论】:

  • containerObj 中包含的对象不会神奇地转换为 两个 对象的数组。请添加显示实际问题的minimal reproducible example

标签: javascript arrays reactjs redux


【解决方案1】:

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

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 2015-10-19
    • 2021-05-09
    • 2016-04-20
    • 2015-06-14
    • 2019-08-21
    • 1970-01-01
    相关资源
    最近更新 更多