3

I'm using Adobe ExtendScript Toolkit to debug. I'm coding for InDesign and really don't have deep JavaScript knowledge (and don't know what ES6 means). ExtendScript doesn't accept let as reserved word. Maybe something about this ES6 you're meaning?

I need to compare all the items in an array and join some of them if one of its child are the same. Let me explain:

Based on the following array:

var array = [[1,"luis"] , [2,"felipe"] , [2,"erick"] , [2,"mark"] , [3,"dustin"]]

So, I want to get as result this array:

var array = [[1,"luis"] , [2,"felipe; erick; mark"] , [3,"dustin"]]

How can I compare the array items to get the desired result?

I already tried two for loops and a while. Maybe I made them wrong. Because of that, I'm here to ask you guys.

If I use this:

for (var i=0; i<array.length-1; i++) {
    for (var j=i+1; j<array.length; j++) {
        if (array[i][0] == array[j][0]) {
            array[i][1] = array[i][1] + "; " + array[j][1];
            }
        }
    }

I have all results. Like this:

1,luis
2,felipe; erick; mark
2,erick; mark
2,mark
3,dustin
Yuri Khristich
  • 13,448
  • 2
  • 8
  • 23

7 Answers7

1

Use Object.entries and reduce.

var array = [[1,"luis"] , [2,"felipe"] , [2,"erick"] , [2,"mark"] , [3,"dustin"]];
const res = Object.entries(array.reduce((a, [n, s]) => (a[n] = a[n] ? a[n] + "; " + s : s, a), {}));
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

You can use reduce

var array = [
  [1, "luis"],
  [2, "felipe"],
  [2, "erick"],
  [2, "mark"],
  [3, "dustin"]
]
const result = array.reduce(function(re, arr) {
  const item = re.find(function(o) {
    return o[0] === arr[0]
  })
  typeof item === 'undefined' ? re.push(arr) : (item[1] = item[1] + ';' + arr[1])
  return re
}, [])
console.log(result)
xianshenglu
  • 4,943
  • 3
  • 17
  • 34
0

You can use Object.entries() and Array's .reduce() methods to get the desired output:

const data = [[1,"luis"] , [2,"felipe"] , [2,"erick"] , [2,"mark"] , [3,"dustin"]];

const result = Object.entries(data.reduce((r, [key, val]) => {
  r[key] = key in r ? [r[key].toString() + "; " + val] : val;
  return r;
}, {}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
0

You can use reduce and map

  • First group by first element in subarray
  • map entries to desired format

var array = [[1,"luis"] , [2,"felipe"] , [2,"erick"] , [2,"mark"] , [3,"dustin"]]

let op = array.reduce((op,inp)=>{
  op[inp[0]] = op[inp[0]] || []
  op[inp[0]].push(inp[1])
  return op
},{})

let final = Object.entries(op).map(([key,value])=> [+key,value.join('; ')])

console.log(final)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
0

Here is the solution below

var array = [[1,"luis"] , [2,"felipe"] , [2,"erick"] , [2,"mark"] , [3,"dustin"]]

function mergeArrayBySameId(){
  let old = [];
  let mergedArr = [];
  array.map(function(curr, i){
    let tmp = "";
    let current = array[i];
    let prev    = ( i === 0 ) ? [] : array[i-1];

    if( current[0] === prev[0] ){
       tmp    = current[0];
       mergedArr[0] = tmp;

       if( !mergedArr.includes( prev[1] ) ){
          mergedArr.push( prev[1] );
       }

       mergedArr.push( current[1] );

    }
    old = prev;

  });

  let mergedStr = ""
  let mergedArrId = mergedArr[0];
  mergedArr.map(function(val){
     if( typeof val !== 'number' ){
          mergedStr += val + "; ";
     }
  });
  mergedArr = [];
  mergedArr[0] = mergedArrId;
  mergedArr[1] = mergedStr;

  function checkId(id){
    return mergedArr[0] != id[0];
  }

  let filterArray = array.filter(checkId);
  let finalArray = filterArray.concat([mergedArr]).sort();
  
  return finalArray;
}

console.log( mergeArrayBySameId() );
Haseeb Afsar
  • 619
  • 5
  • 7
0

Thank you all for your suggestions. I'm sorry because I'm very beginner and just have familiarity with javascript while coding for Adobe software automation. I found a way using:

var array = [[1,"luis"] , [2,"felipe"] , [2,"erick"] , [2,"mark"] , [3,"dustin"]];
for (var i=0; i<array.length-1; i++) {
    var j = i+1;
    while (j < array.length) {
        if (array[i][0] == array[j][0]) {
            array[i][1] = array[i][1] + "; " + array[j][1];
            array.splice(j , 1);
            j = i;
            }
        j++;
        }
    }
alert(array.join("\r"));
0
let arr = [
    [1, "luis"],
    [3, "felipe"],
    [2, "erick"],
    [3, "mark"],
    [3, "dustin"]
];

let res = arr.reduce((acc,cur) => {
    const item = acc.find((obj) => {
        if(obj[0] === cur[0]){
            obj.push(cur[1]);
            return obj
        }
    });
    if(!item)
        acc.push(cur);
    return acc
},[]);
Syed Afzal
  • 150
  • 1
  • 8