简体   繁体   English

如何从 object 中提取值并将其返回到具有特定要求的字符串数组中

[英]How to extract values from an object and return it in an array of strings with specific requirements

I'm trying to build a function that will extract the values from an object and creates an array of strings.我正在尝试构建一个 function ,它将从 object 中提取值并创建一个字符串数组。

In the snippet below, you can see an example of what I did until now but that is not fully correct to my requirements.在下面的代码片段中,您可以看到我到目前为止所做的一个示例,但这并不完全符合我的要求。

Let's say I have data as in the example假设我有示例中的数据

const data = {
      age: {
        min: '17',
        max: '66'
      },
      gender: ['male', 'female'],
      rosacea: 'true',
      rosacea_papulo_pustulosa: 'true',
      severe_rosacea: 'true',
      nurse_contact: 'true',
    };

Right now I'm getting an array of strings for every single value in fact the result if you run it is现在我正在为每个单个值获取一个字符串数组,实际上如果你运行它的结果是

[
  "17",
  "66",
  "male",
  "female",
  "true",
  "true",
  "true",
  "true"
]

But what I need is the following array based if we have or not a nested object inside the data但是我需要的是以下数组,如果我们在数据中是否有嵌套的 object


[
  * This is the result of age min: '17' and max: '66'
  "17 - 66",
  * This is the result of gender male and female
  "male - female",
  * The rest is ok as no nested objects
  "true",
  "true",
  "true",
  "true"
]

The above result is what I'm searching上面的结果是我正在搜索的

Another example as this below data另一个例子如下数据

{disease":{"valid":["MDD","PTSD"],"invalid":["None"]}}

// result expected
[
 "MDD - PTSD",
 "None"
]

My issue at the moment is how to get into the expected results plus needs to be a - between the aggregated results.我目前的问题是如何进入预期结果加上需要在汇总结果-

We can also have a situation where data looks like this我们也可能遇到数据看起来像这样的情况

{ageGroup: ["1","2","3", ..., "n"]}

// result expected
[
 "1 - 2 - 3 ... etc ..."
]

The snippets abut my first attempt片段紧贴我的第一次尝试

 const data = { age: { min: '17', max: '66' }, gender: ['male', 'female'], rosacea: 'true', rosacea_papulo_pustulosa: 'true', severe_rosacea: 'true', nurse_contact: 'true', }; const getValues = (data, values = []) => { if (typeof data.== 'object') { return [..,values; data]. } return Object.values(data),flatMap((v) => getValues(v; values)); }. console.log(getValues(data))

Update更新

The nested objects will never go more deeply as per following example根据以下示例,嵌套对象永远不会更深入地 go

age: {
  group1: {
    min: '1',
    max: '6'
  }
  group2: {
    min: '7',
    max: '10'
  }    
  },

Expected result as预期结果为

[
'1 - 6',
'7 - 10'
]

The OP already came up with a recursive approach. OP 已经提出了一种递归方法。

In order to achieve the OP's expected result(s), one has to implement the recursively operating function in a way that it will concatenate any array-type's item and any of an object-type's value, regardless of the (currently processed) data-structure's nesting level.为了实现 OP 的预期结果,必须执行递归操作 function 以连接任何数组类型的项目和任何对象类型的值,而不管(当前处理的)数据如何 -结构的嵌套级别。

And for the not to be concatenated first level entries of the OP's data object one just needs to map the very object's values by said recursive function.并且对于不连接 OP data object 的第一级条目,只需通过所述递归map map 对象的values

 function stringifyDataRecursively(data) { let result = ''; if (data && (typeof data === 'object')) { if (Array.isArray(data)) { result = data.map(stringifyDataRecursively).join(' - '); } else { result = Object.values(data).map(stringifyDataRecursively).join(' - '); } } else { result = data; } return result; } const sampleData = { age: { min: '17', max: '66' }, gender: ['male', 'female'], rosacea: 'true', rosacea_papulo_pustulosa: 'true', severe_rosacea: 'true', nurse_contact: 'true', }; console.log( 'stringified `sampleData`...', Object.values(sampleData).map(stringifyDataRecursively) ); const sampleData_2 = { disease: { valid: ['MDD', 'PTSD'], invalid: ['None'], }, }; console.log( 'stringified `sampleData_2`...', Object.values(sampleData_2).map(stringifyDataRecursively) ); console.log( 'stringified `sampleData_2.disease`...', Object.values(sampleData_2.disease).map(stringifyDataRecursively) ); const sampleData_3 = { group1: { min: '1', max: '6', }, group2: { min: '7', max: '10', }, }; console.log( 'stringified `sampleData_3`...', Object.values(sampleData_3).map(stringifyDataRecursively) );
 .as-console-wrapper { min-height: 100%;important: top; 0; }

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

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