18
var userids = userbody.contacts.map(function(obj){

  if((obj.iAccepted=='true')&&(obj.contactAccepted=='true')) {
    console.log(true);
    return obj.userID //return obj.userID
  } 

});

This will give the result like this:

[ '0', '35', '37', '30', '34', '36', '33', '32', undefined, '332', '328', '337', '333', undefined ]

I want to skip the undefined elements in the array.

Sumit Aggarwal
  • 831
  • 2
  • 16
  • 29
  • its return false as a element but i want to skip that i need result as [ '0', '35', '37', '30', '34', '36', '33', '32', '332', '328', '337', '333'] instead of [ '0', '35', '37', '30', '34', '36', '33', '32', false, '332', '328', '337', '333', false ] – Sumit Aggarwal Feb 12 '16 at 07:56
  • 3
    Possible duplicate of [javascript skip element on .map()](https://stackoverflow.com/questions/24806772/javascript-skip-element-on-map) – Frazer Kirkman Dec 13 '17 at 13:53
  • There are much more complete and efficient answers to this question here: https://stackoverflow.com/questions/24806772/javascript-skip-element-on-map – Frazer Kirkman Dec 13 '17 at 13:53

5 Answers5

49

This is what Array.prototype.filter() is for. You need to do this in two steps.

var userids = userbody.contacts
    .filter(contact => contact.iAccepted == 'true' && contact.contactAccepted == 'true')
    .map(contact => contact.userId);

The filter part takes out all unnecessary elements, then map converts the rest to the way you want.

Z4-
  • 1,851
  • 14
  • 17
  • Are you using a deprecated node.js? It works fine for me even with node.js LTS v4.2.4, and in [jsbin](http://jsbin.com/hudivipivu/edit?js,console) as well. – Z4- Feb 12 '16 at 09:45
  • No. /var/www/html/nodeproject/helloworld/index.js:270 .filter(contact => contact.iAccepted == 'true' && contact.contactAccepted ^ SyntaxError: Unexpected token > at Module._compile (module.js:439:25) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:902:3 – Sumit Aggarwal Feb 12 '16 at 09:48
  • 1
    No what? It's obvious that you are using an outdated node.js, you could check by using `node --version`, I'd recommend using at least v4.x. If that's not possible, you couldn't use arrow functions, use ES5 syntax, [babel could help you](https://babeljs.io/repl/#?experimental=false&evaluate=false&loose=false&spec=false&code=var%20userids%20%3D%20userbody.contacts%0D%0A%20%20%20%20.filter(contact%20%3D%3E%20contact.iAccepted%20%3D%3D%20'true'%20%26%26%20contact.contactAccepted%20%3D%3D%20'true')%0D%0A%20%20%20%20.map(contact%20%3D%3E%20contact.userId)%3B%0D%0A) if you are in trouble. – Z4- Feb 12 '16 at 09:56
  • v0.10.25 version of node – Sumit Aggarwal Feb 12 '16 at 10:00
  • Yep that's old, please follow the babel link I posted previously, and use the ES5 style code on the right hand side pane. – Z4- Feb 12 '16 at 10:01
  • var userids = userbody.contacts.filter(function (contact) { return contact.iAccepted == 'true' && contact.contactAccepted == 'true'; }).map(function (contact) { return contact.userId; }); – Sumit Aggarwal Feb 12 '16 at 10:03
  • its give me [undefined,undefined,undefined] – Sumit Aggarwal Feb 12 '16 at 10:03
  • Then something's must be wrong with your input data, [check this bin](http://jsbin.com/detuyineqa/edit?js,console), it gives back the expected output in the browser and in node.js as well... – Z4- Feb 12 '16 at 10:07
1

You can use Array.reduce function to implement Array.map and Array.filter process in one function as follows.

const userIds = userbody.contacts.filter((acc, cur) => {
  if((obj.iAccepted == 'true') && (obj.contactAccepted == 'true')) {
    acc.push(obj.userID);
  }
  return acc;
}, []);
console.log(userIds);
Derek Wang
  • 10,098
  • 4
  • 18
  • 39
1

you can use flatMap

The flatMap() method returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level. It is identical to a map() followed by a flat() of depth 1, but slightly more efficient than calling those two methods separately.

var userids = userbody.contacts.flatMap((obj)=>{
  if((obj.iAccepted=='true')&&(obj.contactAccepted=='true')) {
    return obj.userID 
  } 
  else{
    return[];
  }

});
Yoel
  • 7,555
  • 6
  • 27
  • 59
0

You can filter out the false values:

userids = userids.filter((i) => i);

Or, if you can't use arrow functions:

userids = userids.filter(function(i) { return i; });
Shanoor
  • 13,344
  • 2
  • 29
  • 40
  • my value is not like true or false its a numeric values i want to retuen those userID whose contactaccepted or iaccepted value is true – Sumit Aggarwal Feb 12 '16 at 09:21
  • In javascript, a number different from 0 or a non-empty string is a "truthy" value: http://james.padolsey.com/javascript/truthy-falsey/ – Shanoor Feb 12 '16 at 09:24
  • shan please try to understand i have a json data which have so amny values which is realted contactaccepted true and i accepted true now the things i have to filter those ids which both the variable value is true. i used map function to check that. My main problem if the condition value is true then id return into array but if the condition value is false i don't want to add return anything in that case undefined will autimatically added in the list thats y i use return false now resukt is like [34,56,false,74] but i dont want this i want [34,56,74] nothinh will return in else statement – Sumit Aggarwal Feb 12 '16 at 09:31
-1

Not sure if this is what you're looking for:

var arr = [true,true,false,true,false];


var result = arr.map(function(val,ind,arr){

        if(val === !false){
          return val
        }
})

console.log(result) // true, true , true

https://jsfiddle.net/horonv8a/

William
  • 4,422
  • 17
  • 55
  • 108
  • i don't want to return value, i need to skip that value which is not match the condition right now true as well as false value inserted into array element. – Sumit Aggarwal Feb 12 '16 at 08:00
  • This don't skip the `false` values... I like `reduce` in this case. – 0zkr PM Sep 18 '18 at 21:43