簡體   English   中英

如何使用 Javascript 過濾 Object

[英]How to filter an Object with Javascript

我有一個包含一些對象的數組,如下所示:

Array [
  Object {
    "deleted": null,
    "disabled": null,
    "id": "456",
    "price": 970,
    "size": Object {
      "br": 35,
      "us": 3.5,
    },
    "status": "FOR_SALE",
    "type": "SHOES",
  },
   Object {
        "deleted": null,
        "disabled": null,
        "id": "123",
        "price": 1120,
        "size": Object {
          "br": 35,
          "us": 3.5,
        },
        "status": "FOR_SALE",
        "type": "SHOES",
      },
    ]

我想將來自對象的所有價格存儲在一個變量中,我該怎么做?

const prices = arr.map(o => o.price); // returns a new array with the prices

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

 /*to start with: Array [ Object { "deleted": null, "disabled": null, "id": "456", "price": 970, "size": Object { "br": 35, "us": 3.5, }, "status": "FOR_SALE", "type": "SHOES", }, Object { "deleted": null, "disabled": null, "id": "123", "price": 1120, "size": Object { "br": 35, "us": 3.5, }, "status": "FOR_SALE", "type": "SHOES", }, ] isn't valid syntax for an array of objects. Try this instead: */ myArray = [ {deleted: null, disabled: null, id: "456", price: 970, size: {"br": 35, "us": 3.5 }, status: "FOR_SALE", type: "SHOES" }, {deleted: null, disabled: null, id: "123", price: 1120, size: {br: 35, us: 3.5 }, status: "FOR_SALE", type: "SHOES" }, ] const prices = myArray.map(o => {return o.price}) console.log(prices) var costs = [] myArray.forEach(myFunction); function myFunction(item,index,arr){ costs.push(item.price) } console.log(costs) var result = myArray.filter(item => item.price > 1000); console.log(result)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM