如何解决如何删除数组对象中数组内的项目? javascript
我想在每个 presentsData 对象中添加一个名为 sumP 的字段,并用 presents 项填充它总和。
第二个目标,如果sumP>钱我想删除里面最贵的项目 它不起作用
const presentsData= [
{
name: "Peter",presents: ["coffee","holidays"],money: 7000
},{
name: "Mario",presents: ["car","coal"],money: 300
},{
name: "Amanda",presents: ["computer",{
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:我发现很难找出如何在 presents 中迭代,数组内的对象内的数组。
解决方法
import 'package:async/async.dart' show StreamGroup;
// ...
Stream<dynamic> stream;
var streamList = <Stream>[];
// ...
void addPeriodicCallToA({Duration period}) {
streamList.add(Stream<dynamic>.periodic(period,(x) async {
return await getA();
}));
}
// ...
void addPeriodicCallToB({String symbol,Duration period}) {
streamList.add(Stream<dynamic>.periodic(period,(x) async {
return await getB(symbol: symbol);
}));
}
// ...
addPeriodicCallToA(period: Duration(seconds: 1));
addPeriodicCallToA(period: Duration(symbol: 'yoloandswaglol',seconds: 2));
stream = StreamGroup.merge(streamList);
,
您可以过滤礼物并不断移除最昂贵的礼物,直到有钱购买为止:
const presentsData = [{
name: "Peter",presents: ["coffee","holidays"],money: 7000
},{
name: "Mario",presents: ["car","coal"],money: 300
},{
name: "Amanda",presents: ["computer",{
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)
这就是所需要的:
setValue()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。