如何将对象数组中的字符串的一部分与其他字符串进行比较?
How to compaire parts of a string in an array of objects with other strings?
我在 javascript 中有一个对象数组,例如:
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"}
];
我想检查“作者”中的任何名称(person1、person2 等)是否出现在“editors”中的同一对象中。如果是这种情况 - 将一个新的键值对(“出现”)写入包含 author/editor.
名称的对象
输出应如下所示:
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"}
];
我是编程新手,似乎完全被卡住了。
我想要实现输出,我必须在“作者”和“编辑”上使用正则表达式来分隔值并将两个字符串与每个字符串进行比较。不幸的是,我无法使用正则表达式并在比较值时循环遍历数组。
如能就我的问题提出任何建议,我将不胜感激。
这是一个带有一些解释的解决方案
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;
})
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;
}
});
- 将作者和编辑分成两个数组。
- 找出两个数组之间的所有重复项。
- 将副本分配给您的新对象 属性
occurrences
.
无需正则表达式,您可以使用 split
从 person7||person6
字符串中获取一组人:
const persons = `person7||person6`.split('||');
相反的可以用join
来完成:
['person7', 'person6'].join('||'); //evaluates to person7||person6
现在您所要做的就是遍历 arr_objects
,拆分作者和编辑,计算他们的交集并将其放入事件中:
for(obj of arr_objects) {
const authors = obj.authors.split('||');
const editors = obj.edithors.split('||');
const intersection = computeIntersection(authors, editors);
if(intersection.length > 0) {
obj.occurance = intersection.join('||')
}
}
现在剩下的就是实施 computeIntersection
:
function computeIntersection(array1, array2) {
let intersection = [];
let visited = new Set();
for(e of array1) {
visited.add(e);
}
for(e of array2) {
if(visited.has(e)) {
intersection.push(e);
}
}
return intersection;
}
我在 javascript 中有一个对象数组,例如:
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"}
];
我想检查“作者”中的任何名称(person1、person2 等)是否出现在“editors”中的同一对象中。如果是这种情况 - 将一个新的键值对(“出现”)写入包含 author/editor.
名称的对象输出应如下所示:
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"}
];
我是编程新手,似乎完全被卡住了。 我想要实现输出,我必须在“作者”和“编辑”上使用正则表达式来分隔值并将两个字符串与每个字符串进行比较。不幸的是,我无法使用正则表达式并在比较值时循环遍历数组。
如能就我的问题提出任何建议,我将不胜感激。
这是一个带有一些解释的解决方案
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;
})
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;
}
});
- 将作者和编辑分成两个数组。
- 找出两个数组之间的所有重复项。
- 将副本分配给您的新对象 属性
occurrences
.
无需正则表达式,您可以使用 split
从 person7||person6
字符串中获取一组人:
const persons = `person7||person6`.split('||');
相反的可以用join
来完成:
['person7', 'person6'].join('||'); //evaluates to person7||person6
现在您所要做的就是遍历 arr_objects
,拆分作者和编辑,计算他们的交集并将其放入事件中:
for(obj of arr_objects) {
const authors = obj.authors.split('||');
const editors = obj.edithors.split('||');
const intersection = computeIntersection(authors, editors);
if(intersection.length > 0) {
obj.occurance = intersection.join('||')
}
}
现在剩下的就是实施 computeIntersection
:
function computeIntersection(array1, array2) {
let intersection = [];
let visited = new Set();
for(e of array1) {
visited.add(e);
}
for(e of array2) {
if(visited.has(e)) {
intersection.push(e);
}
}
return intersection;
}