Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.8k views
in Technique[技术] by (71.8m points)

问一下怎么查找对象数组里面的指定数据?

我目前有一个总数据,大体是这样的格式

[{ content: "姓名", id: "1" }, { content: "校长", id: "2" }]

下面还有一个数据是动态添加的,是这样的格式

[{ content: "姓名", id: "1" }]

问题来了,我怎么查找下面这个数组的这个单个数据是不是上面包含的?请大佬给个方法


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

提供一种思路

const list = [{ content: "姓名", id: "1" }, { content: "校长", id: "2" }];
const data = { content: "姓名", id: "1" }
const index = list.findIndex(item => item.content === data.content && item.id === data.id);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...