1

I have below array

    var myArr = [
"01;#Sion",
"02;#Sion",
"03;#Mumbai",
"04;#Mumbai",
"05;#Kolkata",
"06;#Kolkata",
"07;#Chennai",
"08;#Chennai"
];

I need output in array as:

["01;#Sion", "03;#Mumbai", "05;#Kolkata", "07;#Chennai"]

It should select only unique values after ;# .

For Sion, output can be "01;#Sion" or "02;#Sion". Any of them is fine.

This is what I have tried so far:

var myArr = ["01;#Sion", "02;#Sion", "03;#Mumbai", "04;#Mumbai", "05;#Kolkata", "06;#Kolkata", "07;#Chennai", "08;#Chennai"];
var newArr = [];

for (j = 0; j < myArr.length; j++) {
    newArr.push(myArr[j].split(";#")[1]);
}

newArr = NWF$.unique(newArr);

console.log(newArr); // -> ["Chennai", "Kolkata", "Mumbai", "Sion"]
7
  • 1
    So what did you try? Commented Feb 11, 2017 at 13:41
  • $.unique tried, but it yields all 8 outputs. Commented Feb 11, 2017 at 13:42
  • Of course it will - they ARE all unique. You need to do some string-handling, processing. If you show an effort, people are more likely to help. Commented Feb 11, 2017 at 13:43
  • $.unqiue(): "Sorts an array of DOM elements, in place, with the duplicates removed. Note that this only works on arrays of DOM elements, not strings or numbers." Commented Feb 11, 2017 at 13:44
  • I tried splitting each value by ;# and then I got unique values, but, afterwards stuck how to add either '01' or '02' to the 'Sion' value in code. Commented Feb 11, 2017 at 13:46

2 Answers 2

1

Here is your solution mate,

var myArr = ["01;#Sion", "02;#Sion", "03;#Mumbai", "04;#Mumbai", "05;#Kolkata", "06;#Kolkata", "07;#Chennai", "08;#Chennai"];
var newArr = [];
var tempArr = [];
for (j = 0; j < myArr.length; j++) {
    newArr.push(myArr[j].split(";#")    [1]);
    tempArr.push(myArr[j]);
}
var temp = '';
var result = [];
for(i=0;i<newArr.length;i++){
    if(temp == '' || temp != newArr[i]){
       temp = newArr[i];
       result.push(tempArr[i]);
    }
}

console.log(result);

Here is working jsfiddle

Give it a try, it will work.

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

Comments

1

One of the easiest ways to do something like this is create a temporary object where the property names are the values you want to compare.

For your case, if a property doesn't exist yet you add it otherwise you skip it

Once object is complete, iterate all it's properties to create new array

var myArr = [
   "01;#Sion",
   "02;#Sion",
   "03;#Mumbai",
   "04;#Mumbai",
   "05;#Kolkata",
   "06;#Kolkata",
   "07;#Chennai",
   "08;#Chennai"
 ];

 var tmp = {};
 myArr.forEach(function(el) {
   var city = el.split('#')[1];
   if (!tmp[city]) {
     tmp[city] = el;
   }
 });

 console.log('tmp', tmp); 

 var newArr = Object.keys(tmp).map(function(key) {
   return tmp[key];
 });

 console.log('newArr', newArr)

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.