S
S
Stanislav2019-07-28 21:24:05
JavaScript
Stanislav, 2019-07-28 21:24:05

How to get to rebuild the array to get such data?

I don’t understand how to do it easier, maybe you can help.
I created 2 cycles with a promise (Promise.all), after each bypass I wanted to delete the first element of the array, but then I realized that it wouldn’t work and I got confused))
There is an array with words, just for clarity letters left

[
    "a",
    "b",
    "c"
]

It can be from more words (letters)
You need to get the following
[
    "a - b",
    "a - c",
    "b - c"
]

So far, only I have come to such a design
return Promise.all(t.map(async e => {
    return Promise.all(t.map(async e2 => {
        if(e !== e2) return `${e} - ${e2}`
    })).then(e => {
        return e.splice(e.indexOf(undefined)+1)
    }).then(e => {
        return console.log(e)
    })
}))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stockholm Syndrome, 2019-07-28
@ms-dred

data.reduce((acc, curr, i) => [...acc, ...data.slice(i + 1).map((item) => `${curr} - ${item}`)], []);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question