【问题标题】:Logic for listing out the item from array typescript从数组打字稿中列出项目的逻辑
【发布时间】:2019-03-12 17:12:44
【问题描述】:

输入对象是

let arr1 = [
[[{id:1}, {id:2}],
[{id:3}, {id:4}],
[{id:5}]
]
}

需要如下结果

let op = [{id:1}, {id:3},{id:5},{id:2},{id:4}]

逻辑列出基于 第一个数组的第一项,然后是第二个数组的第一项,然后是第三个数组的第一项,然后是第一个数组的第二项......它会像这样。 N * N 数组

【问题讨论】:

  • 发布的问题似乎根本没有包含any attempt 来解决问题。 StackOverflow 期待您 try to solve your own problem first,因为您的尝试有助于我们更好地了解您想要什么。请编辑问题以显示您尝试过的内容,以说明您在minimal reproducible example 中遇到的特定问题。欲了解更多信息,请参阅How to Ask 并拨打tour
  • 向我们展示您迄今为止尝试过的代码。 Stackoverflow 不是让您完成家庭作业的网站

标签: javascript arrays typescript


【解决方案1】:

answer 是您要查找的函数:

"use strict";
function answer(arr) {
    let op = [];
    let i = 0;
    while (true) {
        let hasSome = false;
        for (const item of arr) {
            if (item[i]) {
                op.push(item[i]);
                hasSome = true;
            }
        }
        if (!hasSome)
            return op;
        i++;
    }
}
const arr1 = [
    [{ id: 1 }, { id: 2 }],
    [{ id: 3 }, { id: 4 }],
    [{ id: 5 }]
];
console.log(answer(arr1));

【讨论】:

    猜你喜欢
    • 2021-09-01
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 2017-11-14
    相关资源
    最近更新 更多