[英]JavaScript loop through two arrays and get single array
I have two arrays: 我有两个数组:
var arr = [
{ id: 2, username: 'bill'},
{ id: 3, username: 'ted' }];
var b = ["ted", "bill", "john"];
how can I get one array like this: 我怎么能这样一个数组:
var finalArr = [{id: 2, username: 'bill'},{id:3, username: 'ted'}. {id:0, username: 'john'}]
If user from array b
exist in arr
array to add id and if not id to be 0. 如果来自数组
b
用户存在于arr
数组中,则添加id;如果不存在,则id为0。
I tried something like this, but not working 我尝试过类似的方法,但是没有用
var arr = [{ id: 2, username: 'bill' }, { id: 3, username: 'ted' } ]; var b = ["ted", "bill", "john"]; var finalArr = []; function userExists(username) { var a = arr.some(item => { if (item.username === username) { finalArr.push({ username, id: item.id }) } else { finalArr.push({ username, id: 0 }) } }) } b.forEach(el => { userExists(el) }) console.log(finalArr)
Loop through b
using map
. 使用
map
循环遍历b
。 Check if the username
exists in arr
using find
. 使用
find
检查username
存在于arr
中。 If yes, return the object. 如果是,则返回对象。 Else, return a new object with
id: 0
否则,返回一个
id: 0
的新对象
var arr = [ { id: 2, username: 'bill'}, { id: 3, username: 'ted' }]; var b = ["ted", "bill", "john"]; const output = b.map(username => { const found = arr.find(a => a.username === username); return found || { username, id: 0 } }) console.log(output)
You can use map and find to construct your new array like 您可以使用map和find来构建新的数组,例如
var arr = [{ id: 2, username: 'bill' }, { id: 3, username: 'ted' } ]; var b = ["ted", "bill", "john"]; const finalArr = b.map(username => { const find = arr.find(o => o.username === username); return find ? find : { id: 0, username } }) console.log(finalArr)
You could take a Set
and filter by looking to the set and delete visited items. 你可以采取
Set
和过滤器通过寻找到设置和删除参观项目。 At the end concat missing items. 最后,缺少物品。
This solution repsects the order of the given data and adds the missing object in order of the names
array. 此解决方案代表给定数据的顺序,并按
names
数组的顺序添加缺少的对象。
var data = [{ id: 42, username: 'foo' }, { id: 2, username: 'bill' }, { id: 3, username: 'ted' }], names = ["ted", "bill", "john"], missing = new Set(names), result = data .filter(({ username }) => missing.delete(username)) .concat(Array.from(missing, username => ({ id: 0, username }))); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can do that using reduce method. 您可以使用reduce方法做到这一点。
b.reduce((result, elm) => {
const userObj = arr.find((a) => a.username === elm);
if(userObj) {
result.push(userObj)
} else {
result.push({
id: 0,
username: elm
});
}
return result;
}, []);
Check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce for more details about reduce. 检查https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce了解有关reduce的更多详细信息。
You can craete a dummy array for storing result and , find the existing using find
. 您可以创建一个虚拟数组以存储结果,并使用
find
查找现有数组。 If not found create dummy object and push inside array. 如果未找到,则创建虚拟对象并推入数组内部。
var arr = [ { id: 2, username: 'bill'}, { id: 3, username: 'ted' }]; var b = ["ted", "bill", "john"]; myArr = []; b.forEach(function(element) { var el=arr.find(x => x.username === element); if(el){ myArr.push(el); }else{ var dummy={}; dummy.id=0; dummy.username=element; myArr.push(dummy); } }); console.log(myArr);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.