简体   繁体   中英

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

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

You should retrieve property text of found object:

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

var name = object.text;

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.

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) 

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM