Javascript 如何将对象数组中的字符串部分与其他字符串进行比较?
我有一个javascript对象数组,如:Javascript 如何将对象数组中的字符串部分与其他字符串进行比较?,javascript,arrays,javascript-objects,string-comparison,Javascript,Arrays,Javascript Objects,String Comparison,我有一个javascript对象数组,如: var arr_objects= [ {id: 1, authors: "person1||person2", edithors: "person1||person7"}, {id: 2, authors: "person3", edithors: "person2"}, {id: 3, authors: "person4||person5|
var arr_objects= [
{id: 1, authors: "person1||person2", edithors: "person1||person7"},
{id: 2, authors: "person3", edithors: "person2"},
{id: 3, authors: "person4||person5||person6", edithors: "person7||person6"},
{id: 4, authors: "person7||person6", edithors: "person6"}
];
我想检查“authors”(person1、person2等)中是否有名字出现在“edithors”中的同一个对象中。如果是这种情况,则向包含作者/编辑器名称的对象写入一个新的键值对(“occurance”)
输出应如下所示:
var arr_objects= [
{id: 1, authors: "person1||person2", edithors: "person1||person7", occurance: "person1"},
{id: 2, authors: "person3", editors: "person2" },
{id: 3, authors: "person4||person5||person6", edithors: "person7||person6", occurance: "person6"},
{id: 4, authors: "person7||person6", edithors: "person6", occurance: "person6"}
];
我是编程新手,似乎完全被卡住了。
我想为了实现输出,我必须使用“authors”和“editors”上的正则表达式来分离值,并将两个字符串相互比较。不幸的是,在比较值时,我无法使用正则表达式并在数组中循环
我非常感谢您对我的问题提出的任何建议。这里有一个解决方案,并给出一些解释
arr_objects.map(function(item)
{
// split both authors and editors into an array
let authors = item.authors.split('||');
let editors = item.edithors.split('||');
let occurance = [];
// for each author, check if it also appears among editors
for (a in authors)
{
if (editors.indexOf(authors[a])>=0)
{
//if yes, push it into occurrence buffer
occurance.push(authors[a]);
}
}
// join the array into a string
item.occurance = occurance.join('||');
return item;
})
引用
不需要正则表达式,您可以使用
split
从person7 | | person6
字符串中获取一组人员:
const persons=`person7 | | person6`.split('| |');
使用连接可以实现相反的操作:
['person7','person6'].加入('124; |')//评估为person7 | | person6
现在,您所要做的就是循环遍历arr\u对象
,拆分作者和编辑,计算它们的交集并使其发生:
用于(arr_对象的对象){
const authors=obj.authors.split(“| |”);
const editors=obj.edithors.split(“| |”);
const intersection=computeIntersection(作者、编辑);
如果(交点长度>0){
obj.occurance=交叉点连接(“| |”)
}
}
现在剩下的就是实现computeIntersection
:
函数计算接口(array1,array2){
设交点=[];
让访问=新集合();
(第1列第e项){
添加(e);
}
(e/2排){
如果(已访问,请访问(e)){
交叉口。推(e);
}
}
折返交叉口;
}
您可以使用String.split将authors/edithor转换为数组,然后查找这两个数组的共同元素:谢谢Eriks,这非常有效!你用这个救了我。
var arr_objects= [
{id: 1, authors: "person1||person2", edithors: "person1||person7"},
{id: 2, authors: "person3", edithors: "person2"},
{id: 3, authors: "person4||person5||person6", edithors: "person7||person6"},
{id: 4, authors: "person7||person6", edithors: "person6"}
];
function getOccurance(arr){
// clone
arr = [...arr]
arr = arr.map(a => {
let authors = a.authors.split("||");
let editors = a.edithors.split("||")
let occurance = ""
authors.forEach(auth => {
editors.forEach(ed => {
if(auth == ed) {
if(occurance.length > 0){
occurance += "||" + auth;
}else{
occurance += auth
}
}
})
})
if(occurance.length > 0){
a.occurance = occurance
}
return a
})
return arr
}
console.log(getOccurance(arr_objects))
arr_objects.forEach(obj => {
const authors = obj.authors.split('||');
const editors = obj.edithors.split('||');
const matches = authors.filter(val => editors.includes(val));
if (matches.length) {
obj.occurrences = matches;
}
});