Jacob
Jacob

Reputation: 137

how to get string from array using angularjs or javascript

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

Upvotes: 0

Views: 1027

Answers (7)

Skomantas Čepulis
Skomantas Čepulis

Reputation: 31

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.

Upvotes: 1

G2 Jakhmola
G2 Jakhmola

Reputation: 811

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);

Upvotes: 0

Grzesiek Danowski
Grzesiek Danowski

Reputation: 467

You should retrieve property text of found object:

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

var name = object.text;

Upvotes: 1

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38502

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);

Upvotes: 0

Just code
Just code

Reputation: 13801

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)

Upvotes: 1

Ikenna Anthony Okafor
Ikenna Anthony Okafor

Reputation: 697

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.

Upvotes: 1

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

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);

Upvotes: 2

Related Questions