簡體   English   中英

如何從對象數組中獲取特定數據並存儲在js(vue js)中的新數組中

[英]How to fetch particular data from array of objects and store in new array in js (vue js)

我有這個 object 數組

let detail : [ 
       0: {
            Code: "Code 1"
            Price: "0.00"   
          },           

       1: {
            Code: "Code 2"
            Price: "9.00"   
          }
]

我想將價格存儲在一個數組中(例如:結果),以便我可以將它與對象的另一個現有數組合並(例如:alldetail)

result = [
    0: {
          Price:"0.00"
       },
    1: {
          Price:"9.00"
       },   
]

使用map()方法創建一個新數組,該數組填充了提供的 function 在調用數組中的每個元素上執行的結果。

因此,在您的情況下,您將返回一個帶有鍵 Price 的 object ,其值將是當前的 object 及其 Price 屬性的值。

 let detail = [ { Code: "Code 1", Price: "0.00" }, { Code: "Code 2", Price: "9.00" } ]; let result = detail.map(current => {return {Price: current.Price}}); console.log(result);

使用map創建一個新的對象數組。

 const detail = [ { Code: "Code 1", Price: "0.00" }, { Code: "Code 2", Price: "9.00" } ]; const result = detail.map(({ Price }) => ({ Price })); console.log(result);

附加文件

暫無
暫無

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

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