2

I got an 2d Array filled by a Database like this:

var Index = [["value1_lang1", "value1_lang2", "value1_lang3", "3,6,11,"],["value1_lang1", "value1_lang2", "value1_lang3", "37,41,97,114,"],["value2_lang1", "value2_lang2", "value2_lang3", "9,14,33,"]];

The array will got over 600 values. I try to do it with the following two for loops:

for(var i = 0; i < Index.length; i++) {
 for(var j = i + 1; j < Index.length; ) {
  var item1 = Index[i][0]; //Only have to compare the values from lang1
  var item2 = Index[j][0];

  if(item1 == item2) {
   var page1 = Index[i][3];
   var page2 = Index[j][3];

   if(page1 != page2) {
    var newpages = page1 + page2;
   } else {
    var newpages = page1;
   }

   Index[i][3] = newpages;
   Index.splice(j, 1);
   page1 = "";
   page2 = "";
   newpages = "";

  } else {
   j++;
  }
 }
}

The result should look like this:

var Index = [["value1_lang1", "value1_lang2", "value1_lang3", "3,6,11,37,41,97,114"], ["value2_lang1", "value2_lang2", "value2_lang3", "9,14,33,"]];

But the real result looks like this:

var Index = [[undefined, undefined, undefined, undefined],["value1_lang1", "value1_lang2", "value1_lang3", "3,6,11,37,41,97,114,"], ["value2_lang1", "value2_lang2", "value2_lang3", "9,14,33,"]];
9
  • 1
    Your result has bracket mismatch, hence, not clear with the desired result. Can you please correct that? Commented Nov 18, 2016 at 11:01
  • 1
    Did you mean if(item1 == item2) ? Commented Nov 18, 2016 at 11:03
  • 1
    @nikhil thanks for the hint! Commented Nov 18, 2016 at 11:06
  • @DeepakKumar Yes! Commented Nov 18, 2016 at 11:07
  • 1
    I'm able to run your code in Chrome console with change of y to j. And I'm getting the desired variable Index as output. Can you check again? Also make the change newpages = page1 + "," + page2 Commented Nov 18, 2016 at 11:14

2 Answers 2

1

You could use the first three elements of the inner array as key for a hash table and push to the result set, if not exist or append the element at index 3.

var data = [["value1_lang1", "value1_lang2", "value1_lang3", "3,6,11,"], ["value1_lang1", "value1_lang2", "value1_lang3", "37,41,97,114,"], ["value2_lang1", "value2_lang2", "value2_lang3", "9,14,33,"]],
    result = [];

data.forEach(function (a) {
    var key = a.slice(0, 3).join('|');
    if (!this[key]) {
        this[key] = a.slice();
        result.push(this[key]);
        return;
    }
    this[key][3] += a[3];
}, Object.create(null));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

1

Based on my assumption on the requirement, following should work for you

var Index = [
  ["value1_lang1", "value1_lang2", "value1_lang3", "3,6,11"],
  ["value1_lang1", "value1_lang2", "value1_lang3", "37,41,97,114"],
  ["value2_lang1", "value2_lang2", "value2_lang3", "9,14,33"]
];

var map = {}; // Creating a map of unique values and value (will get updated)

for (var item of Index) {
  var val = map[item[0]];
  if (val) {
    val[3] = val[3] + item.slice(3).join(",");
  } else {
    val = item;
  }
  map[item[0]] = val;
}

var result = []; // Will be a collection of desired values
for (var key in map) {
  result.push(map[key]);
}

console.log(JSON.stringify(result));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.