3

I have a basic problem with Javascript. This code is below.

var users = [{"user":{"id":1,"username":"google"}},{"user":{"id":2,"username":"yahoo"}}]
const result = users.filter(users.id === 1);
console.log(result);

I want to get result if user_id exist in array or not.

The error is:

false is not a function
allo
4,2878 gold badges45 silver badges76 bronze badges
asked Nov 27, 2017 at 8:19
1

6 Answers 6

2

You need to take a callback, like an arrow function, with the right properties to the id, because you have a nested structure for every user.

var users = [{ user: { id: 1, username: "google" } }, { user: { id:2, username: "yahoo" } }];
const result = users.filter(user => user.user.id === 1);
console.log(result);

answered Nov 27, 2017 at 8:21

1 Comment

Thank you for your help!
2

You are writing the function in a wrong way

Try like this

const result = users.filter(x => x.user.id === 1);
console.log(result);

DEMO

answered Nov 27, 2017 at 8:22

1 Comment

Thank you for your help!
2

You need to pass a callback to the function. This will return you the items which id is 1.

const users = [{"user":{"id":1,"username":"google"}},{"user":{"id":2,"username":"yahoo"}}]
const result = users.filter(user => user.user.id === 1);
console.log(result);

Also instead of checking the length, you can use some function. This will terminate check of the rest items if any item was found.

const users = [{"user":{"id":1,"username":"google"}},{"user":{"id":2,"username":"yahoo"}}]
const result = users.some(user => user.user.id === 1);
console.log(result);

answered Nov 27, 2017 at 8:21

2 Comments

Thank you for your help!
This solution is best for me.!!
1

You might want to use reduce instead:

var users = [{"user":{"id":1,"username":"google"}},{"user":{"id":2,"username":"yahoo"}}]
const result = users.reduce((s, user) => s || user.user.id === 1, false);
console.log(result);

.filter returns an array, while .reduce can return whatever you want (in this case, a boolean).

If you want to use .filter, try:

var users = [{"user":{"id":1,"username":"google"}},{"user":{"id":2,"username":"yahoo"}}]
const result = !!users.filter(user => user.user.id === 1).length;
console.log(result);

However, keep in mind that .filter will create an extra array and might not be as efficient as .reduce.

answered Nov 27, 2017 at 8:25

3 Comments

Thank you for your help!
i will vote the best for your answer. Can you add solution with .filter return Boolean
@FeedGit Sure. Added.
1

Try like this

var users = [{"user":{"id":1,"username":"google"}},{"user":{"id":2,"username":"yahoo"}}]
const result = users[0].user.id;
console.log(result);

answered Nov 27, 2017 at 8:24

1 Comment

Thank you for your help!
0
var users = [{"user":{"id":1,"username":"google"}},{"user":
{"id":2,"username":"yahoo"}}]
const result = users.filter(function(el){
return (el.user.id===1)
});
console.log(result[0].user.username);
//This will print the username of the object with id==1 in array
answered Nov 27, 2017 at 8:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.