简体   繁体   English

将 [key, value] 的 'value' 配对成数组

[英]Make 'value' of [key, value] Pair into Array

I have a simple object I want to turn into key value pairs to display in a SectionList .我有一个简单的对象,我想将其转换为键值对以显示在SectionList

const DOBBY = {
  foo: 'bar',
  toto: ['hoge', 'piyo'],
};

I used Objects.entries() to turn it into an array of objects:我使用Objects.entries()将其转换为对象数组:

const toArray = Object.entries(DOBBY).map(([ title, data ]) => ({ title, data }));

So that I get back:所以我回来了:

[
  { title: 'foo', data: 'bar' },
  { title: 'toto', data: ['hoge', 'piyo'] },
];

Right now SectionList splits 'bar' into three rows.现在SectionList'bar'分成三行。
How do I put 'bar' into an array so that I get:如何将'bar'放入数组中,以便得到:

[
 { title: 'foo', data: ['bar'] },
 { title: 'toto', data: ['hoge', 'piyo'] },
]

I think Array.of() will do it, but I don't know where to implement it, or whether there's something I can do with .map()我认为Array.of()会做到,但我不知道在哪里实现它,或者我是否可以用.map()做一些事情

You can simply force nest data into a sub-array, and then flatten it using Array.prototype.flat :您可以简单地将嵌套data强制放入子数组中,然后使用Array.prototype.flat将其展平:

  • When data is an array, [data] will be an array with a single element that is also an array, so Array.flat will flatten it into an arraydata是一个数组时, [data]将是一个具有单个元素的数组,该数组也是一个数组,因此Array.flat会将其扁平化为一个数组
  • When data is a string, [data] will be an array of string, and Array.flat will preserve the way it isdata是一个字符串时, [data]将是一个字符串数组,而Array.flat将保持Array.flat

 const DOBBY = { foo: 'bar', toto: ['hoge', 'piyo'], }; const toArray = Object.entries(DOBBY).map(([ title, data ]) => ({ title, data: [data].flat() })); console.log(toArray);

Of course, you can also go for a slightly long winded way, but that means you don't unneedingly nest array within an array:当然,您也可以采用稍微冗长的方式,但这意味着您不必在数组中不必要地嵌套数组:

 const DOBBY = { foo: 'bar', toto: ['hoge', 'piyo'], }; const toArray = Object.entries(DOBBY).map(([ title, data ]) => { return { title, data: Array.isArray(data) ? data : [data] }; }); console.log(toArray);

The Array.prototype.flat() method is of course slower, because you will be performing additional array nesting. Array.prototype.flat()方法当然更慢,因为您将执行额外的数组嵌套。 That's a sacrifice you pay for readability.这是您为可读性付出的牺牲。 Also, for very small objects and arrays, this performance overhead is probably negligible:此外,对于非常小的对象和数组,这种性能开销可能可以忽略不计:

https://jsperf.com/array-prototype-flat-vs-isarray/ https://jsperf.com/array-prototype-flat-vs-isarray/

+---------------------------------+------------+------------+------------+
|               Test              | Chrome 80  | Firefox 74 | Safari 13  |
+---------------------------------+------------+------------+------------+
| Array.prototype.flat() approach | 640k ops/s | 2.4m ops/s | 590k ops/s |
| Array.isArray approach          | 2.6m ops/s | 2.7m ops/s | 650k ops/s |
+---------------------------------+------------+------------+------------+

Just check if it is an Array, if so just insert the data, if not put the data into an empty Array只检查它是否是一个数组,如果是则插入数据,如果不是则将数据放入空数组

const DOBBY = {
  foo: 'bar',
  toto: ['hoge', 'piyo'],
};

const toArray = Object.entries(DOBBY)
.map(([title, data ]) => ({ title, data: Array.isArray(data) ? data : [data] }));

it worked for me, so i hope this helps :)它对我有用,所以我希望这会有所帮助:)

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

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