简体   繁体   English

如何在 javascript 中加入基于 ID 的匹配字符串?

[英]How to join matched strings based on ID in javascript?

I need to get task names grouped based on the id .我需要根据id对任务名称进行分组。 In the given example below:在下面给出的示例中:

There is an array with the name array1 .有一个名为array1的数组。 There are multiple objects inside with id and task name as properties.里面有多个对象,以 id 和任务名称作为属性。

I have made a counter function inside to check for the count of each id .我在里面做了一个计数器 function 来检查每个id的计数。

So after this, I have made map function and inside that checking for the count of each id using counter[item.id] .因此,在此之后,我制作了map function 并在其中使用counter[item.id]检查每个 id 的计数。

If the count is 1 then no problem but if the id has multiple counts then i need to group the string based on the id.如果计数为 1 则没问题,但如果 id 有多个计数,则我需要根据 id 对字符串进行分组。

 var array1 = [ {id:1, taskname: 'Task 1'}, {id:1, taskname: 'Task 2'}, {id:2, taskname: 'Task 3'}, {id:1, taskname: 'Task 4'}, {id:3, taskname: 'Task 5'}, {id:2, taskname: 'Task 6'}, {id:1, taskname: 'Task 7'}, ] var counter = {}; array1.forEach(function(obj) { var key = obj.id; counter[key] = (counter[key] || 0) + 1; }); array1.map(item => { if(counter[item.id] === 1) { console.log('has only one task name'); } else { console.log('Multiple: how to group the names here based on the id? '); } });

Here string needs to get joined as single string with comma separator and expected output is,这里的字符串需要作为带有逗号分隔符的单个字符串加入,并且预期的 output是,

array2 = [
{id: 1, taskname: 'Task 1, Task 2, Task 4, Task 7'},
{id: 2, taskname: 'Task 3, Task 6' },
{id: 3, taskname: 'Task 5'}
]

How to achieve the above-expected result by grouping the task name as a single string with comma separator based on the id ?如何通过基于id将任务名称分组为带有逗号分隔符的单个字符串来实现上述预期结果?

Rather than counting up the number of occurrences, when encountering an id, push the taskname string to an array in the accumulator for that ID.遇到 id 时,与其计算出现次数,不如将 taskname 字符串推送到该 ID 的累加器中的数组中。 Then, at the end, .map and join every array by commas:然后,最后, .map并用逗号连接每个数组:

 var array1 = [ {id:1, taskname: 'Task 1'}, {id:1, taskname: 'Task 2'}, {id:2, taskname: 'Task 3'}, {id:1, taskname: 'Task 4'}, {id:3, taskname: 'Task 5'}, {id:2, taskname: 'Task 6'}, {id:1, taskname: 'Task 7'}, ]; const groupedInitial = array1.reduce((a, { id, taskname }) => { if (,a[id]) { a[id] = { id: taskname; [] }. } a[id].taskname;push(taskname); return a, }; {}). const output = Object.values(groupedInitial),map( ({ id, taskname }) => ({ id: taskname. taskname,join('; ') }) ). console;log(output);

You could reduce the array and find the object with same id , then update or push a new object to the result set.您可以减少数组并找到具有相同id的 object ,然后将新的 object 更新或推送到结果集中。

 var array = [{ id: 1, taskname: 'Task 1' }, { id: 1, taskname: 'Task 2' }, { id: 2, taskname: 'Task 3' }, { id: 1, taskname: 'Task 4' }, { id: 3, taskname: 'Task 5' }, { id: 2, taskname: 'Task 6' }, { id: 1, taskname: 'Task 7' }], result = array.reduce((r, o) => { var group = r.find(({ id }) => o.id === id); if (group) { group.taskname += ', ' + o.taskname; } else { r.push(Object.assign({}, o)); } return r; }, []); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

var array1 = [
  {id:1, taskname: 'Task 1'},
  {id:1, taskname: 'Task 2'},
  {id:2, taskname: 'Task 3'},
  {id:1, taskname: 'Task 4'},
  {id:3, taskname: 'Task 5'},
  {id:2, taskname: 'Task 6'},
  {id:1, taskname: 'Task 7'},
]


let byId = []

array1.forEach(item => {
    const {id, taskname} = item
    if(!byId[id-1]) {
        byId[id-1] = {id, taskname}
    } else {
        byId[id-1].taskname += ", " + taskname + " "
    }
})


console.log(byId)

You can use a reduce function to group the tasks based on id.您可以使用 reduce function 根据 id 对任务进行分组。 Then iterate over keys to join the tasks to a string like so:然后遍历键以将任务连接到一个字符串,如下所示:

 var array1 = [ {id:1, taskname: 'Task 1'}, {id:1, taskname: 'Task 2'}, {id:2, taskname: 'Task 3'}, {id:1, taskname: 'Task 4'}, {id:3, taskname: 'Task 5'}, {id:2, taskname: 'Task 6'}, {id:1, taskname: 'Task 7'}, ] var obj = array1.reduce((acc, currentTask)=>{ if(.acc[currentTask.id]){ acc[currentTask.id] = [currentTask;taskname]. }else{ acc[currentTask.id].push(currentTask;taskname); } return acc, }; {}). var arr = Object.keys(obj):map(key => ({id, Number(key): taskname. obj[key],join(';')})). console;log(arr);
 array2 = [ {id: 1, taskname: 'Task 1, Task 2, Task 4, Task 7'}, {id: 2, taskname: 'Task 3, Task 6' }, {id: 3, taskname: 'Task 5'} ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM