1

I'm trying to remove part of the value of an element in an array after '_' and once its done remove all repeated elements.

Array looks like

names =  ["ann_w", "james_q", "ann_q", "peter_p", "steve_q", "james_s"];

And I am trying to convert it into

names = ["ann", "james", "peter", "steve"];

Below code is returning undefined and cant figure it out how to remove all repeated elements.

for (i = 0; i < names.length; i++) {
   names[i] = names[i].substring(names[i].indexOf("_") + 1);
}
1
  • yeah you are right, i forgot to put it when bringing the code her Commented May 30, 2017 at 16:06

6 Answers 6

4

use Set & map:

var names =  ["ann_w", "james_q", "ann_q", "peter_p", "steve_q", "james_s"];

var unique_names = [ ...new Set(names.map(name => {
   return name.split('_')[0]
}))]
console.log(unique_names)

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

5 Comments

@up , tl;dr : Set guarantee uniqueness, while "..." turns Set back into Array
Syntactic sugar to decrease code: var unique_names = [ ...new Set(names.map(name => name.split('_')[0]))];
[ ...new Set crashes in safari and IE, what would be the alternative?
You could always transpile this beautiful code with babel
2
var names = ["ann_w", "james_q", "ann_q", "peter_p", "steve_q", "james_s"];

var tmp = names.map(function(n){
   return n.split("_")[0];
}).sort();

// tmp = ["ann", "ann", "james", "james", "peter", "steve"]

var result = [];

tmp.forEach(function(t){
    if(!result.includes(t))
        result.push(t);
});

console.log(result);

1 Comment

Works for me :)
0

You can try using split on the string instead of doing the substring, then you'd get an array of strings, and since you only want the first name, you'd take array[0].

Also, you're getting back errors because you're trying to substring the entire array instead of a string. You'd need to do names[i] = names[i].substring

Comments

0

You can do this: Use array#reduce and Object.keys. reduce will create an object with name as keys and then you can get the key as an array via Object.keys().

var names =  ["ann_w", "james_q", "ann_q", "peter_p", "steve_q", "james_s"];

var obj = names.reduce(function(r,v) {
  var name = v.split('_')[0];
  if(!r[name]) {
    r[name] = 1;
  }
  return r;
}, {});

console.log(Object.keys(obj));

Comments

0
var names =  ["ann_w", "james_q", "ann_q", "peter_p", "steve_q", "james_s"];
var finalArray = [];
for (var i = 0; i <names.length; i++){
var substringArray = names[i].split("_");
if(finalArray.indexOf(substringArray[0]) == -1) finalArray.push(substringArray[0]);
}

https://jsfiddle.net/3z3naanb/9/

Comments

0
names =  ["ann_w", "james_q", "ann_q", "peter_p", "steve_q", "james_s"];

for (i = 0; i < names.length; i++) {
   names[i] = names[i].substring(0, names[i].indexOf("_"));
}

var unique = names.filter(function(item, i, names) {
    return i == names.indexOf(item);
});

console.log(unique)

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.