1

I'm stuck of finding a way to consolidate array elements.

so my array is in format of [id1:port1,id2:port2,id1:port3,id2:port4,id5:port5...] where each element has 2 portions. The id portion is not unique. what I try to consolidate is to create a new array will have data like [id1#port1:port3,id2#port2:port4,id5#port5]

I tried code below but it didn't get me too far. can any guru help me out?

var orinString = "id1:port1,id2:port2,id1:port3,id2:port4,id5:port5";

var newArray1 = orinString.split(",");
var newArray2 = orinString.split(",");
var newArray3 = [];

for (x=0; x<=newArray1.length-1; x++) {
  for (y=0; y<= newArray2.length-1; y++) {
    if ((newArray1[x].split(":")[0] == newArray2[y].split(":")[0]) && (newArray1[x].split(":")[1] != newArray2[y].split(":")[1])) {
      newArray3.push(newArray1[x].split(":")[0] +"#"+ newArray1[x].split(":")[1]);
    }
  }
}

for (z=0; z<=newArray3.length; z++) {
  gs.log("show me the result " +newArray3[z]);
}

2
  • What do you mean by "consolidate"? Commented Nov 7, 2015 at 3:57
  • maybe I should use word manipulate, sorry I really don't know the proper term but what I want to get from raw format of [id1:port1,id2:port2,id1:port3,id2:port4,id5:port5...] to [id1#port1:port3,id2#port2:port4,id5#port5] Commented Nov 7, 2015 at 4:01

2 Answers 2

1

is it that you want:

var orinString = "id1:port1,id2:port2,id1:port3,id2:port4,id5:port5";
var arr1 = orinString.split(",");
var temp= ""; 
var newStr = "";
arr1.sort();
for(i=0; i< arr1.length; i++) {
    var item = arr1[i].split(':');
    if(item[0] !== temp || temp === "") {
        newStr += "," + item[0] + "#" + item[1];
    } else {
        newStr += ":"+item[1];
    }
    temp = item[0];
}
console.log(newStr.substring(1));
Sign up to request clarification or add additional context in comments.

1 Comment

That's exactly what I'm looking for. Thank you!
0

A typical way to solve a problem like this is

  • Convert them into workable values
  • Populate some kind of lookup table
  • Output the results of this lookup table

For example

var orinString = "id1:port1,id2:port2,id1:port3,id2:port4,id5:port5";

var idsAndPorts = orinString.split(",");

// Populate a key lookup
var hashTable = {};
idsAndPorts.forEach(function(s) {
  var splitValue = s.split(':');
  var key = splitValue[0];
  var value = splitValue[1];
  if(hashTable[key]) {
    hashTable[key].push(value);
  } else {
    hashTable[key] = [value];
  }
});

// Now convert it back into an array again
var finalArray = [];
for(var k in hashTable) {
  finalArray.push(k + '#' + hashTable[k].join(','));
}

// View the results
finalArray.forEach(function(f) {
  console.log(f);
})

This does not guarantee the final array will be sorted, but you can sort it yourself if you wish.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.