Saya ingin menambahkan bidang di dalam setiap objek presentsData yang disebut sumP dan mengisinya dengan jumlah item presents.

Tujuan kedua, jika sumP > uang saya ingin menghapus item paling mahal di dalamnya Ini tidak bekerja

const presentsData= [
        {
          name: "Peter",
          presents: ["coffee","holidays"],
          money: 7000
        },
        {
          name: "Mario",
          presents: ["car","coal"],
          money: 300
        },
        {
          name: "Amanda",
          presents: ["computer","coal"],
          money: 300
        },
        {
          name: "David",
          presents: ["clothes", "car"],
          money: 2000
        }
    ]
    const prices= [
        {
          present: "coffee",
          price: 1
        },
        {
          present: "holidays",
          price: 1000
        },
        {
          present: "videogames",
          price: 40
        },
        {
          present: "computer",
          price: 600
        },
        {
          present: "tattoo",
          price: 30
        },
        {
          present: "clothes",
          price: 80
        },
        {
          present: "car",
          price: 6000
        },
        {
          present: "phone",
          price: 800
        },
        {
          present: "motorbike",
          price: 3500
        },
         {
          present: "coal",
          price: 0
        }
      ]
    
    const pricesMap = new Map(prices.map(({ present, price }) => [present, price]))
    
    const res1 =  presentsData.map(s=>{
    return {...s,
           sumP: s.presents.reduce((acc, p) => acc + pricesMap.get(p),0)
           }
    })
    
     console.log("this is res1=>",res1) //this is presentsData result after adding the field sumP
    
    console.log(pricesMap)
    
    const res2 = res1.map((r)=>{
      if(r.sumP > r.money){
      ( /* would like to delete the item  inside "presents" with the bigger price using pricesMap */)
      }
    })
    
    console.log(res2)

Pd: yang menurut saya sulit adalah mencari tahu cara mengulangi present , array di dalam objek di dalam array.

0
foxDev 10 Januari 2021, 22:35

3 jawaban

Jawaban Terbaik

Anda dapat menyaring hadiah dan terus menghapus hadiah paling mahal sampai ada uang untuk membelinya:

const presentsData = [{
    name: "Peter",
    presents: ["coffee", "holidays"],
    money: 7000
  },
  {
    name: "Mario",
    presents: ["car", "coal"],
    money: 300
  },
  {
    name: "Amanda",
    presents: ["computer", "coal"],
    money: 300
  },
  {
    name: "David",
    presents: ["clothes", "car"],
    money: 2000
  }
]
const prices = [{
    present: "coffee",
    price: 1
  },
  {
    present: "holidays",
    price: 1000
  },
  {
    present: "videogames",
    price: 40
  },
  {
    present: "computer",
    price: 600
  },
  {
    present: "tattoo",
    price: 30
  },
  {
    present: "clothes",
    price: 80
  },
  {
    present: "car",
    price: 6000
  },
  {
    present: "phone",
    price: 800
  },
  {
    present: "motorbike",
    price: 3500
  },
  {
    present: "coal",
    price: 0
  }
]

const pricesMap = new Map(prices.map(({
  present,
  price
}) => [present, price]))

const withSumP = presentsData.map(s => {
  return { ...s,
    sumP: s.presents.reduce((acc, p) => acc + pricesMap.get(p), 0)
  }
})

console.log(withSumP) //this is presentsData result after adding the field sumP

const mostExpensive = (arr) => {
  return arr.reduce((acc, el) => {
    if (pricesMap.get(el) > pricesMap.get(acc)) {
      return el
    }
    return acc;
  })
}

// If you want to remove the most expensive present once for each element
const result = withSumP.map((el) => {
  if (el.sumP > el.money) {
    const presentToDelete = mostExpensive(el.presents);
    return { ...el,
      presents: el.presents.filter(p => p !== presentToDelete)
    }

  }
  return el;
})

console.log(result);

// If you want to delete presents until you can buy them and also update sumP to be in sync with the presents:

const resultAlt = withSumP.map((el) => {
  let updatedPresents = [...el.presents];
  let updatedSumP = el.sumP;
  while ((updatedSumP > el.money) && (updatedPresents.length > 0)) {
    const presentToDelete = mostExpensive(updatedPresents);
    updatedPresents = updatedPresents.filter(p => p !== presentToDelete);
    updatedSumP -= pricesMap.get(presentToDelete);
  }
  return { ...el,
    presents: updatedPresents,
    sumP: updatedSumP
  };
})

console.log(resultAlt)
1
Ramesh Reddy 10 Januari 2021, 20:14
const deleteMostExpensive = (array, prices) => {
    let res = [];
    let mostEspensive = array[0];
    array.forEach(product => {
        let cur = prices.find(p => p.present == product)
        if (cur.price > mostEspensive.price)
            mostEspensive = cur;
    })

    array.forEach(e => {
        if (e != mostEspensive.present)
            res.push(e);
    })

    return res;
}


const calculSumP = (elt, prices) => {
    r = 0;
    elt.presents.forEach(e => {
        r += prices.find(c => c.present == e).price;
    })
    return r;
}


presentsData.forEach(elt => {
    elt.sumP = calculSumP(elt, prices);

   while (elt.sumP > elt.money) {
       let newPresents = deleteMostExpensive(elt.presents, prices);
       elt.presents = newPresents;
       elt.sumP = calculSumP(elt, prices)
   }
})
1
Thomas Saison 10 Januari 2021, 19:55

Ini semua yang diperlukan:

const items = (prices) => {
  let obj = {};
  for (p of prices) {
    obj[p.present] = p.price;
  }
  return obj;
}

var priceList = items(prices);

presentsData.forEach( (person) => {
 let presents = person.presents;
 person.sumP = presents.reduce( (acc, current) => {
   return acc + priceList[current];
 }, 0);
 if (person.sumP > person.money) {
   presents.sort( (a,b) => priceList[a] - priceList[b]);
   presents.pop();
 }
});
1
TimonNetherlands 10 Januari 2021, 20:31