简体   繁体   English

如何使用angularjs或javascript从数组获取字符串

[英]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 返回我{id:2,文本:“ two”}期望两个仅字符串没有其他内容

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 . 您可以先查找对象,然后通过检查find()操作是否实际上返回了一个对象或undefined对象来获取text属性。

 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. 如果您确定该localStorage值存在对象,则也可以使用解构直接从find()获取该text值。 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 . 您所做的返回位于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. 如果要获取文本,则在以var name返回元素之后,只需执行name.text因为array.find()返回通过逻辑运算的元素。

You can use filter and map . 您可以使用filtermap 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. 如果您喜欢使用es6语法,则可以这样编写。 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 : 来自https://developer.mozilla.org/zh-CN/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. find()方法返回满足提供的测试功能的数组中第一个元素的值。 Otherwise undefined is returned. 否则返回undefined。

Try like this way to get text attribute value while using find by id 尝试以这种方式在使用id查找时获取text属性值

 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 find将返回满足条件的对象

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



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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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