从数组中提取值到一个新的单一数组。

huangapple go评论148阅读模式
英文:

Pull values from array to a new single array

问题

以下是翻译好的内容:

  1. 我有一个这样的数组
  2. pa: [{
  3. id: 1,
  4. sa: [1, 2, 3]
  5. }, {
  6. id: 2,
  7. sa: [1, 2, 3]
  8. }, {
  9. id: 3,
  10. }]
  11. 其中一些对象包含一个 `sa` 数组而另一些则没有我想创建一个新数组其中包含所有 `sa` 中的数字放入一个新的单一数组中
  12. 我尝试了以下方法但它会添加多个数组
  13. for (let i = 0; i < pa.length; i++) {
  14. if (pa[i].sa !== undefined && pa[i]?.sa) {
  15. f.push(pa[i]?.sa?.map(a => a.id));
  16. }
  17. }
  18. 我希望最终的数组是
  19. newArray = [1, 2, 3, 1, 2, 3]
英文:

I have an array like this:

  1. pa: [{
  2. id: 1,
  3. sa: [ 1, 2, 3 ]
  4. }, {
  5. id: 2,
  6. sa: [ 1, 2, 3 ]
  7. }, {
  8. id: 3,
  9. }]

Some of the objects contain an sa array and some don't. I want to create a new array that contains all the numbers of sa in a new single array

I have tried this but it will add in multiple arrays

  1. for (let i = 0; i &lt; pa.length; i++) {
  2. if (pa[i].sa!== undefined &amp;&amp; pa[i]?.sa) {
  3. f.push(p[i]?.sa?.map(a=&gt; a.id));
  4. }
  5. }

i want the final array to be

  1. newArray = [1,2,3,1,2,3]

答案1

得分: 3

Sure, here is the translated code portion:

  1. const
  2. data = { pa: [{ id: 1, sa: [1, 2, 3] }, { id: 2, sa: [1, 2, 3] }, { id: 3 }] },
  3. result = data.pa.flatMap(({ sa = [] }) => sa);
  4. console.log(result);

Please let me know if you need any further assistance with this code.

英文:

You could take a flat mapping with a destructuring with optional array.

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
data = { pa: [{ id: 1, sa: [1, 2, 3] }, { id: 2, sa: [1, 2, 3] }, { id: 3 }] },
result = data.pa.flatMap(({ sa = [] }) => sa);

  1. console.log(result);

<!-- end snippet -->

答案2

得分: 1

你可以通过将.flat()应用于你的结果来获得一个单一的数组。

一个简化版本的你的代码:

  1. const newArray = pa.map(e => e.sa ?? []).flat();
英文:

You can get a single array by just applying .flat() to your result.

A simpler version of your code:

  1. const newArray = pa.map(e =&gt; e.sa ?? []).flat();

答案3

得分: 1

以下是您要翻译的内容:

"有点讽刺的是,使用for循环的方法将比其他建议运行得更快,因为它不会创建数组的中间副本,并且在主观上与其他方法一样易读。

for (let i = 0, sa; i < pa.length; i++)
if (sa = pa[i].sa)
for (let j = 0; j < sa.length; j++)
newArray.push(sa[j]);"

英文:

Somewhat ironically, the approach with for loops will run a lot quicker than the other suggestions because it doesn't create intermediate copies of the array, and is subjectively just as readable.

  1. for (let i = 0, sa; i &lt; pa.length; i++)
  2. if (sa = pa[i].sa)
  3. for (let j = 0; j &lt; sa.length; j++)
  4. newArray.push(sa[j]);

答案4

得分: 0

  1. let output = [];
  2. let k = pa.map(item => {
  3. if (item.sa) {
  4. output = [...output, ...item.sa];
  5. }
  6. })
英文:

Not sharing an optimized answer as few answers are already their. Just using your approach correctly so that you know how your logic can be used.

  1. let output = [];
  2. let k = pa.map(item =&gt; {
  3. if (item.sa) {
  4. output = [...output, ...item.sa];
  5. }
  6. })

huangapple
  • 本文由 发表于 2023年7月4日 20:15:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76612515.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定