简体   繁体   English

如何从 React Native 中未定义键值的数组中删除 object?

[英]How to remove object from an array which have a key value as undefined in React Native?

I have a list which after selecting values returns an array.我有一个列表,在选择值后返回一个数组。 Now, suppose I select 1st and 3rd values skipping 2nd one then the array returned is as:现在,假设我 select 第一个和第三个值跳过第二个,那么返回的数组如下:

 [
  {
   "cname": "10TKN", 
   "code": "403", 
   "id": 1
  },
  {
   "cname": undefined, 
   "code": undefined, 
   "id": 2
  }, 
  {
   "cname": "20TKN", 
   "code": "403", 
   "id": 3
  }
 ]

I want the expected result as:我希望预期的结果为:

 [
   {
    "cname": "10TKN", 
    "code": "403", 
    "id": 1
   },
   {
    "cname": "20TKN", 
    "code": "403", 
    "id": 3
   }
 ]

You can use JavaScript's Array.filter() to filter away undefined values.您可以使用 JavaScript 的Array.filter()过滤掉undefined的值。

 const data = [ { "cname": "10TKN", "code": "403", "id": 1 }, { "cname": undefined, "code": undefined, "id": 2 }, { "cname": "20TKN", "code": "403", "id": 3 } ]; const res = data.filter(({cname, code, id }) => cname && code && id); console.log(res);

This will ensure that only objects with truthy values are returned.这将确保只返回具有真实值的对象。

Alternatively, as mentioned by HMR on the comments, if you only want to check for undefined , it will be better to do this:或者,正如 HMR 在评论中提到的那样,如果您只想检查undefined ,最好这样做:

const res = data.filter(({cname, code, id }) => cname !== undefined && code !== undefined && id !== undefined);

Assuming the key that we want to evaluate is cname .假设我们要评估的键是cname We need to filter out the array using Array.filter function -:我们需要使用Array.filter function - 过滤掉数组:

 let arr = [ { "cname": "10TKN", "code": "403", "id": 1 }, { "cname": undefined, "code": undefined, "id": 2 }, { "cname": "20TKN", "code": "403", "id": 3 } ]; arr = arr.filter(({cname}) => cname); console.log(arr)

You can use filter as suggested already but you will have to check for the selected id(s) from list and then discard the ones not selected.您可以按照建议使用filter ,但您必须从列表中检查选定的 ID,然后丢弃未选择的 ID。 In short you will return true only if the list object id matches with the one you selected简而言之,只有当列表 object id 与您选择的匹配时,您才会返回 true

const dataList = [
  {
   "cname": "10TKN", 
   "code": "403", 
   "id": 1
  },
  {
   "cname": undefined, 
   "code": undefined, 
   "id": 2
  }, 
  {
   "cname": "20TKN", 
   "code": "403", 
   "id": 3
  }
 ];

 const selectedDataIds = [1, 3];
 const res = dataList.filter(({ id }) => selectedDataIds.includes(id));
 console.log(res);

You can use Destructuring assignment and skip some values with a comma, like this:您可以使用Destructuring assignment并用逗号跳过一些值,如下所示:

 const arr = [ { "cname": "10TKN", "code": "403", "id": 1 }, { "cname": undefined, "code": undefined, "id": 2 }, { "cname": "20TKN", "code": "403", "id": 3 } ] const [first, ,third] = arr; console.log(first, third);

Destructuring assignment - Ignoring some returned values 解构赋值 - 忽略一些返回值

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

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