0
var array = [
      {id: 1, text: "one"},
      {id: 2, text: "two"},
      {id: 3, text: "three"},
      {id: 4, text: "four"},
      {id: 5, text: "five"}
    ];

var name = array.find(function(item){
          return item.id == $localStorage.id;
 });

returns me {id: 2, text: "two"} expected two only string nothing else should print

3
  • 1
    find will return the object that satisfy the condition Commented Oct 30, 2018 at 8:49
  • Another note: if id is unique you can use js object intstead array: var data = {"1": "one", "2" : "two", "3": "three", "4": "four", "5": "five"}; and then write: var name = data[$localStorage.id]; It will be simpler and faster. Commented Oct 30, 2018 at 9:27
  • Add .text after }) like this: var name = array.find(function(item) { return item.id == $localStorage.id; }).text; Commented Oct 30, 2018 at 10:47

7 Answers 7

2

You can first find the object and then get the text property by checking if the find() operation actually returned a object or it is undefined.

var array = [{
    id: 1,
    text: "one"
  },
  {
    id: 2,
    text: "two"
  },
  {
    id: 3,
    text: "three"
  },
  {
    id: 4,
    text: "four"
  },
  {
    id: 5,
    text: "five"
  }
];

var findObj = array.find(function(item) {
  return item.id == 2;
});
//check if findObj is defined or not
var name = findObj? findObj.text: null;
console.log(name);

You can also use destructuring to get that text value directly from find() if you are sure the object exist for that localStorage value. Otherwise, it will raise error.

var array = [{
    id: 1,
    text: "one"
  },
  {
    id: 2,
    text: "two"
  },
  {
    id: 3,
    text: "three"
  },
  {
    id: 4,
    text: "four"
  },
  {
    id: 5,
    text: "five"
  }
];

var {text} = array.find(function(item) {
  return item.id == 2;
});
console.log(text);

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

Comments

1

What you did returns the element at the position where item.id == $localStorage.id. If you want to get the text, then after the element is returned in var name, you just do name.text because array.find() returns the element that passed the logical operation.

Comments

1

You can use filter and map. This way you can customize your filtered result the way you want.

var array = [
      {id: 1, text: "one"},
      {id: 2, text: "two"},
      {id: 3, text: "three"},
      {id: 4, text: "four"},
      {id: 5, text: "five"}
    ];

var name = array.filter(a=> a.id === 2).map(b=> {return b.text});
console.log(name)

7 Comments

@Thomas, this will work for both the cases single and multiple filters, later on when you have multiple conditions like id and text both, this will be helpful.
imo. Premature optimization. And I don't know wether I've ever had to combine a filter on a list with an element lookup by ID. For me it's ususally one or the other.
@G2Jakhmola why not?
It surely depends on the size of the array and its purpose however I think filter and map is an overkill in this case.
@LazioTibijczyk I don't think so it would overkill. Can you explain?
|
1

You should retrieve property text of found object:

var object = array.find(function(item){
   return item.id === $localStorage.id;
});

var name = object.text;

2 Comments

Will definitely not work. `array.find returns the element at the array index where thee boolean returns true.
The naming of the variable "object" is not ideal but it will work.
1

If you like to use es6 syntax, you can write like this. You did everything good except, you needed to get specific object value.

const array = [
  { id: 1, text: "one" },
  { id: 2, text: "two" },
  { id: 3, text: "three" },
  { id: 4, text: "four" },
  { id: 5, text: "five" }
];

// So here i use same find as you did.

let object = array.find(item => {
  return item.id == $localStorage.id;
});

// And assigning text property of object to variable 'name'
// since object, can be undefined, using OR empty object, 
// so no error will be thrown if so.

let { text: name } = object || {};
console.log(name);

from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find:

The find() method returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.

Comments

0

Try like this way to get text attribute value while using find by id

var array = [{
    id: 1,
    text: "one"
  },
  {
    id: 2,
    text: "two"
  },
  {
    id: 3,
    text: "three"
  },
  {
    id: 4,
    text: "four"
  },
  {
    id: 5,
    text: "five"
  }
];

var name = array.find(function(item) {
  return item.id == 2;
}).text;

console.log(name);

Comments

0

find will return the object that satisfy the condition

var object = array.find(function(item) {
  return item.id == $localStorage.id;
});



var name = object? object.text: null;
console.log(name);

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.