7 Mart 2019 Perşembe

Object.values metodu

Giriş
Nesnenin içindeki tüm property'leri dizi olarak verir.
Örnek
Elimizde bir object dizisi olsun.
let orders = [
  {  amount: '100', user: 'admin', date: 'March 6, 2019' },
  {  amount: '120', user: 'admin', date: 'March 6, 2019' },
  {  amount: '80', user: 'admin', date: 'March 7, 2019' },
  {  amount: '200', user: 'admin', date: 'March 7, 2019' },
];
Bunıu array of array haline getirmek isteyelim.
orders = [
  ['100', 'admin', 'March 6, 2019'],
  ['120', 'admin', 'March 6, 2019'],
  ['80', 'admin', 'March 7, 2019'],
  ['200', 'admin', 'March 7, 2019'],
];
Şöyle yaparız.
const result = orders.map(Object.values);
Örnek
Şöyle yaparız.
var arr = [{modelNumber: "123456789", balance: { amount:1000, currency:"EUR" }},
           {modelNumber: "987654321", balance: { amount:2000, currency:"EUR" }},
           ...,
          ];

var tempObj = {};
arr.forEach((obj)=>{
  if(tempObj[obj.balance.currency]){
    tempObj[obj.balance.currency].totalAmount += obj.balance.amount
  } else {
    tempObj[obj.balance.currency] = {
      currency: obj.balance.currency,
      totalAmount : obj.balance.amount
    }
  }
});
var resArray = Object.values(tempObj);
console.log(resArray);

Hiç yorum yok:

Yorum Gönder