从 javascript 中的 2 个数组对象中获取不匹配的数据



我有数组,1个数组是主数据,其他数组包含管道分隔值。 请在下面找到相同的代码

var master = [
{id:1, value:'John'},
{id:2, value:'Bobby'}
];
var names = [
{id:1, name:'Sandra|John', type:'user', username:'sandraJ'},
{id:2, name:'John', type:'admin', username:'johnny2'},
{id:3, name:'Peter|John', type:'user', username:'peteJ'},
{id:4, name:'Bobby', type:'user', username:'be_bob'},
{id:4, name:'Peter1|John1', type:'user', username:'be_bob'}
];

生成的输出应如下所示

var result3 = [
{id:1, name:'Sandra'},
{id:2, name:'Peter'},
{id:2, name:'Peter1|John1'}
];

我尝试遵循 ES6 版本,但它没有抛出预期的输出。

let result = names.filter(o1 => !master.some(o2 => 
o1.name.split('|').includes(o2.value)));

我也尝试用每个替换一些,但它仍然不起作用

let result = names.filter(o1 => !master.every(o2 => 
o1.name.split('|').includes(o2.value)));

有人可以帮我做同样的事情吗?

这确实假设结果中的 id 不正确,因为我不知道彼得|约翰和彼得1|约翰 1 将其 ID 更改为 2。

const master = [
{id:1, value:'John'},
{id:2, value:'Bobby'}
];
const names = [
{id:1, name:'Sandra|John', type:'user', username:'sandraJ'},
{id:2, name:'John', type:'admin', username:'johnny2'},
{id:3, name:'Peter|John', type:'user', username:'peteJ'},
{id:4, name:'Bobby', type:'user', username:'be_bob'},
{id:4, name:'Peter1|John1', type:'user', username:'be_bob'}
];
// map the valid names for easier access
const valid_names = master.map(({ value }) => value );
// We could map => filter instead if that's clearer.
const invalid_items = names.reduce(( invalid_items, item ) => {
// Get all the diferent names inside the item.
const item_names = item.name.split( '|' );
// Filter out all the valid names
const invalid_names = item_names.filter( name => !valid_names.includes( name ));
// If there are invalid names remaining, create a new object.
// No idea how the "id" property should be transformed.
if ( invalid_names.length ) {
invalid_items.push({
id: item.id,
name: invalid_names.join( '|' )
});
}
return invalid_items;
}, [] );
console.log( invalid_items );

这是一个使用filterfindIndex的版本。

const master = [
{id:1, value:'John'},
{id:2, value:'Bobby'}
];
const names = [
{id:1, name:'Sandra|John', type:'user', username:'sandraJ'},
{id:2, name:'John', type:'admin', username:'johnny2'},
{id:3, name:'Peter|John', type:'user', username:'peteJ'},
{id:4, name:'Bobby', type:'user', username:'be_bob'},
{id:4, name:'Peter1|John1', type:'user', username:'be_bob'}
];
//This step, if you wish that the original data should not be mutated.
const namesCopy = Object.assign([], names);
//Filter through the names
const result = namesCopy.filter(obj=>{
//Split the name if there is a |
const split = obj.name.split("|");
//Create a new name without the master names in it
const newName = split.filter(name=>{
//check to see if the master name exists within the name
return master.findIndex(obj2=>obj2.value === name) === -1;
}).join("|"); // Join them together as a string if there is more than one name left

//If newName is empty then we can assume that master names existed in this object
if(newName.length === 0) return false;

//otherwise update the name with newName
obj.name = newName;
return true;
});
console.log(result);

这样的事情怎么样?

使用 .map、.filter 和 for 循环。

var master = [
{id:1, value:'John'},
{id:2, value:'Bobby'}
];
var names = [
{id:1, name:'Sandra|John', type:'user', username:'sandraJ'},
{id:2, name:'John', type:'admin', username:'johnny2'},
{id:3, name:'Peter|John', type:'user', username:'peteJ'},
{id:4, name:'Bobby', type:'user', username:'be_bob'},
{id:4, name:'Peter1|John1', type:'user', username:'be_bob'}
];
let yFilter = master.map(itemY => { return itemY.value; });
let filteredX = names.filter(itemX => !yFilter.includes(itemX.name));
var processed = [];
for(var i = 0; i < filteredX.length; i++){
	var item = filteredX[i];
var contains = master.filter(function(x){ return item.name.split("|").indexOf(x.value) > -1; });

if(contains.length > 0){
	item.name = item.name.substring(0, item.name.indexOf(contains[0].value) - 1);
}
processed.push(item);
}
console.log(processed);

您可以使用过滤后的名称映射对象,然后按名称的长度进行过滤。最后,使用更新的name属性映射新对象。

var master = [{ id: 1, value: 'John' }, { id: 2, value: 'Bobby' }],
names = [{ id: 1, name: 'Sandra|John', type: 'user', username: 'sandraJ' }, { id: 2, name: 'John', type: 'admin', username: 'johnny2' }, { id: 3, name: 'Peter|John', type: 'user', username: 'peteJ' }, { id: 4, name: 'Bobby', type: 'user', username: 'be_bob' }, { id: 4, name: 'Peter1|John1', type: 'user', username: 'be_bob' }],
result = names
.map((s => o =>
[o, o.name.split('|').filter(n => !s.has(n)).join('|')]
)(new Set(master.map(({ value }) => value))))
.filter(([, { length }]) => length)
.map(([o, name]) => Object.assign({}, o, { name }));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

相关内容

  • 没有找到相关文章

最新更新