我有一个数组,一个数组是主数据,另一个数组包含管道分隔值。请在下面找到相同的代码
cp
结果输出应为以下
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'}
];
我尝试使用ES6以下版本,但没有抛出预期的输出。
var result3 = [
{id:1, name:'Sandra'},
{id:2, name:'Peter'},
{id:2, name:'Peter1|John1'}
];
我也尝试过用每一个代替一些,但是仍然不起作用
let result = names.filter(o1 => !master.some(o2 =>
o1.name.split('|').includes(o2.value)));
有人可以帮我吗?
答案 0 :(得分:4)
这确实假设结果中的ID不正确,因为我不知道否则Peter | John和Peter1 | John1如何将其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 );
答案 1 :(得分:3)
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);
答案 2 :(得分:1)
这样的事情怎么样?
使用.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);
答案 3 :(得分:1)
您可以使用过滤后的名称映射对象,然后过滤名称的长度。最后,映射具有更新的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; }