如何删除数组对象中数组内的项目? javascript

如何解决如何删除数组对象中数组内的项目? 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?