简体   繁体   English

如何将对象数组转换为仅包含一个属性值的数组?

[英]How do I convert an array of objects to an array including only one property's values?

I am trying to use the.includes( ) array method to check if an item is in my JSON file.我正在尝试使用 .includes( ) 数组方法来检查某个项目是否在我的 JSON 文件中。 So far I have made a connection to the database (using JSON server npm plugin) and have retrieved the array of objects back, and stored them in the variable "fetchedData".到目前为止,我已经连接到数据库(使用 JSON 服务器 npm 插件)并检索了对象数组,并将它们存储在变量“fetchedData”中。 Now, I know that.includes( ) takes an array as the first argument and searches within the array for the second argument.现在,我知道 .includes() 将数组作为第一个参数并在数组中搜索第二个参数。 How do I convert this array of objects to an array including just the name property values?如何将此对象数组转换为仅包含名称属性值的数组? Is this how.includes( ) works?这是 how.includes( ) 的工作原理吗?

This is the JSON file:这是 JSON 文件:

  "savedexercises": [
    {
      "name": "all fours squad stretch",
      "target": "quads",
      "gifUrl": "http://d205bpvrqc9yn1.cloudfront.net/1512.gif",
      "id": 1
    },
    {
      "name": "ankle circles",
      "target": "calves",
      "gifUrl": "http://d205bpvrqc9yn1.cloudfront.net/1368.gif",
      "id": 2
    },
    {
      "name": "arm slingers hanging bent knee legs",
      "target": "abs",
      "gifUrl": "http://d205bpvrqc9yn1.cloudfront.net/2355.gif",
      "id": 3
    }
  ]
}

I am just trying to access all the name property values and then store them into an array.我只是想访问所有名称属性值,然后将它们存储到一个数组中。

You can you filter and map to find all the values of a particular key and transform it to an array of objects or any form.您可以过滤和 map 以查找特定键的所有值并将其转换为对象数组或任何形式。 Includes will only tell you if such a key exists. includes 只会告诉您是否存在这样的密钥。 Refer the code below参考下面的代码

  const createArraybasedOnkey= (key) => {
    console.log(
      obj.savedexercises
        .filter((x) => x[key])
        ?.map((x) => {
          return { [key]: x[key] };
        })
    );
  };

code sandbox: https://codesandbox.io/s/pedantic-rain-u1t9th?file=/src/App.js:625-817代码沙箱: https://codesandbox.io/s/pedantic-rain-u1t9th?file=/src/App.js:625-817

Use array.map() .使用array.map() For example:例如:

const exerciseNames = savedexercises.map(exercise => exercise.name)
// Expected output:
// ["all fours squad stretch", "ankle circles",
//  "arm slingers hanging bent knee legs"]

// Now you can check if the exerciseNames includes a certain exercise:
console.log(exerciseNames.includes("all fours squad stretch"))
// Expected output: true

暂无
暂无

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

相关问题 如何将JSON中的对象数组转换为仅包含值的数组? - How can I convert an array of objects in JSON to an array with values only? 将具有相同属性的对象数组转换为具有数组值的一个对象 - Convert array of objects with same property to one object with array values 如何基于另一个数组中的键/值对为一个数组中的对象分配属性值? - How do I assign property values for objects in one array based on key/value pairs in another? 如何在 JavaScript 中将对象数组转换为一个对象? - How do I convert array of Objects into one Object in JavaScript? 如何将对象数组转换为 JavaScript 中的一个 Object? - How do I convert array of Objects to one Object in JavaScript? 将值数组转换为具有该值作为属性的对象数组 - Convert array of values into array of objects with that value as a property 如何将一个对象数组拆分成多个普通数组,其中每个数组都由一个属性的值填充? - How to split an array of objects into multiple normal arrays where each array is populated by one property's values? 如何过滤嵌套在数组对象属性中的对象数组? - How do i filter array of objects nested in property of array objects? 如何将对象数组转换为对象的对象? - How do i convert an array of objects to objects of objects? 如何按属性将对象数组分组为 ARRAYS 数组? - How do I group an array of objects into an array of ARRAYS by a property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM