使用foreach

时间:2019-03-26 12:06:06

标签: javascript arrays

我正在尝试将所有数量字段更改为值:数组的每个成员都为1,但是我尝试返回未定义的值!

var array = [{
  "quantity": 3,
  "line_item_id": 2140047343716,
  "location_id": null,
  "restock_type": "no_restock",
  "price": "30000.00",
  "subtotal": "90000.00",
  "total_tax": "15000.00",
  "discounted_price": "30000.00",
  "discounted_total_price": "90000.00",
  "total_cart_discount_amount": "0.00"
}, {
  "quantity": 2,
  "line_item_id": 2140047310948,
  "location_id": null,
  "restock_type": "no_restock",
  "price": "81.92",
  "subtotal": "163.84",
  "total_tax": "27.31",
  "discounted_price": "81.92",
  "discounted_total_price": "163.84",
  "total_cart_discount_amount": "0.00"
}]


var refundItems = array.forEach(element => {
  element.quantity = 1
})
console.log(refundItems);

我之前做过forEach循环,我认为这会起作用,我是否遗漏了一些明显的东西?

更新->

我以为我会进行更新,并更具体地说明我的情况,该数组是作为React应用程序中的props来实现的(想象一下'array'为props.refunds':

const FinalMessage = (props) => {

由于这个原因,我需要编写一个新数组(因为我想将数据分配给该对象:

        let data = {
        domain: props.domain,
        customer_details: props.data.orderInfo,
        shipping: props.data.resdata.result.refund.shipping,
        refund_line_items: array,
        transactions: props.data.resdata.result.refund.transactions
    }

如果我尝试了不返回任何内容的forEach,那么它仍然会使用原始数据。

谢谢您的帮助!

4 个答案:

答案 0 :(得分:4)

map返回一个数组,其中包含您从其内部返回的所有内容,即具有已编辑数量的元素。

forEach遍历给定数组,但不希望返回且不会返回任何内容。

要使用地图在refundItems中建立一个新的编辑项数组,但要避免使原始数组发生变化。

var array = [{
  "quantity": 3,
  "line_item_id": 2140047343716,
  "location_id": null,
  "restock_type": "no_restock",
  "price": "30000.00",
  "subtotal": "90000.00",
  "total_tax": "15000.00",
  "discounted_price": "30000.00",
  "discounted_total_price": "90000.00",
  "total_cart_discount_amount": "0.00"
}, {
  "quantity": 2,
  "line_item_id": 2140047310948,
  "location_id": null,
  "restock_type": "no_restock",
  "price": "81.92",
  "subtotal": "163.84",
  "total_tax": "27.31",
  "discounted_price": "81.92",
  "discounted_total_price": "163.84",
  "total_cart_discount_amount": "0.00"
}]

var refundItems = array.map(element => ({...element, quantity: 1}))

console.log(refundItems)
console.log(array)

使用forEach

var array = [{
  "quantity": 3,
  "line_item_id": 2140047343716,
  "location_id": null,
  "restock_type": "no_restock",
  "price": "30000.00",
  "subtotal": "90000.00",
  "total_tax": "15000.00",
  "discounted_price": "30000.00",
  "discounted_total_price": "90000.00",
  "total_cart_discount_amount": "0.00"
}, {
  "quantity": 2,
  "line_item_id": 2140047310948,
  "location_id": null,
  "restock_type": "no_restock",
  "price": "81.92",
  "subtotal": "163.84",
  "total_tax": "27.31",
  "discounted_price": "81.92",
  "discounted_total_price": "163.84",
  "total_cart_discount_amount": "0.00"
}]

const refundItems = [] 
array.forEach(element => {
  refundItems.push({...element, quantity: 1})  
})

console.log(refundItems)
console.log(array)

答案 1 :(得分:2)

forEach不返回值。您可以在对象数组的forEach循环中操作对象。因此,如果这是您想要的,就可以像这样

  

forEach()方法为每个数组元素执行一次提供的函数。

const array = [
  {
    "quantity" : 5,
    "somekey" : 'somekey'
  },
  {
    "quantity" : 10,
    "somekey2" : 'somekey2'
  }
]


array.forEach(element => element.quantity = 1)
console.log(array);

如果您不想更改初始数组,请使用map并使用spread ...语法(或Object.assign())来复制对象。您需要复制对象,因为在map创建新数组的同时,更改对象属性值也会更改“旧”数组中的对象。

  

map()方法创建一个新数组,其结果是在调用数组中的每个元素上调用提供的函数。

const array = [
  {
    "quantity" : 5,
    "somekey" : 'somekey'
  },
  {
    "quantity" : 10,
    "somekey2" : 'somekey2'
  }
]
const newArray = array.map(obj => {
  return {
    ...obj, // use spread to duplicate the original object
    'quantity' : 1
    }
})
console.log(newArray,'newArray')
console.log(array,'oldArray')

答案 2 :(得分:0)

根据您的问题,您尚未指定要更新值的新数组还是需要更新值的同一数组。

首先,按照javascript的forEach定义,forEach始终返回 undefined 。它不会返回任何东西。因此,您不会在 refundItems 中得到任何东西。

以下代码将更改forEach迭代的原始数组变量的值。

var array = [{
"quantity":3,
"line_item_id":2140047343716,
"location_id":null,
"restock_type":"no_restock",
"price":"30000.00",
"subtotal":"90000.00",
"total_tax":"15000.00",
"discounted_price":"30000.00",
"discounted_total_price":"90000.00",
"total_cart_discount_amount":"0.00" } ,
{"quantity":2,
"line_item_id":2140047310948,
"location_id":null,
"restock_type":"no_restock",
"price":"81.92",
"subtotal":"163.84",
"total_tax":"27.31",
"discounted_price":"81.92",
"discounted_total_price":"163.84",
"total_cart_discount_amount":"0.00"
}]

array.forEach(element => {
  element.quantity = 1
})
console.log(array);

因此,与其使用其他变量的值并将其用于控制​​台,不如使用 array 变量本身,您将获得所需的输出。

答案 3 :(得分:-1)

您可以在foreach中lineItem个元素:

index