简体   繁体   中英

How to make a arrays mixing on react-redux?

So i'm trying to mix a bunch of my arrays.

For example, if i have an: array1 = ["Banana", "Orange", "Apple", "Mango"];

and

array2 = ["Saab", "Volvo", "BMW"];

i want to get

array3 = ["Banana Saab", "Banana Volvo", "Banana BMW", "Orange Saab", "Orange Volvo", "Orange BMW", "Apple Saab", "Apple Volvo", "Apple BMW", "Mango Saab", "Mango Volvo", "Mango BMW"]

Well i've done it on python, but it's hard to me to understand how to do it on react.

That's how it looks on python for me. It looks horibble, but works good and fast:

        first_handle = [i for i in combination.first_column.split('\n') if i] or "'"
        second_handle = [i for i in combination.second_column.split('\n') if i] or "'"
        third_handle = [i for i in combination.third_column.split('\n') if i] or "'"
        fourth_handle = [i for i in combination.fourth_column.split('\n') if i] or "'"
        fifth_handle = [i for i in combination.fifth_column.split('\n') if i] or "'"
        sixth_handle = [i for i in combination.sixth_column.split('\n') if i] or "'"
        seventh_handle = [i for i in combination.seventh_column.split('\n') if i] or "'"
        eighth_handle = [i for i in combination.eighth_column.split('\n') if i] or "'"
        itog = ['{} {} {} {} {} {} {} {}'.format(x1, x2, x3, x4, x5, x6, x7, x8)
                for x1 in first_handle for x2 in second_handle for x3 in third_handle
                for x4 in fourth_handle for x5 in fifth_handle for x6 in sixth_handle for x7 in seventh_handle
                for x8 in eighth_handle]
        new_itog = set(itog)

So is there any solutions for me?

I don't ask you to write the code for me(but I would be glad if you did :D)

But is there any examples or projects you know which can help me to find a solution?

This has nothing to do with react. It's a simple array iteration.

 const array1 = ["Banana", "Orange", "Apple", "Mango"] const array2 = ["Saab", "Volvo", "BMW"] const results = [] for(let item of array1){ for(let subitem of array2){ results.push(`${item} ${subitem}`) } } console.log(results) 

You can use nested map and flat

 let array1 = ["Banana", "Orange", "Apple", "Mango"]; let array2 = ["Saab", "Volvo", "BMW"]; let final = array1.map(initial=> array2.map(val=> initial + ' ' + val)).flat() console.log(final) 

Typescript

let Array1: Array<string> = ["Banana", "Orange", "Apple", "Mango"];
let Array2: Array<string> = ["Saab", "Volvo", "BMW"];

var Assigned = (A1:Array<string>, A2:Array<string>): Array<string> => {
    let A = [];A2.map(S => { A1.map(T => { A.push(`${T} ${S}`)}) });return A;
}

console.log(Assigned(Array1, Array2));

Javascript

var Array1 = ["Banana", "Orange", "Apple", "Mango"];
var Array2 = ["Saab", "Volvo", "BMW"];
var Assigned = function (A1, A2) {
    var A = [];
    A2.map(function (S) { A1.map(function (T) { A.push(T + " " + S); }); });
    return A;
};
console.log(Assigned(Array1, Array2));

Output

[
  'Banana Saab',  'Orange Saab',
  'Apple Saab',   'Mango Saab',
  'Banana Volvo', 'Orange Volvo',
  'Apple Volvo',  'Mango Volvo',
  'Banana BMW',   'Orange BMW',
  'Apple BMW',    'Mango BMW'
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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