简体   繁体   English

如何使数组在react-redux上混合?

[英]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"]; 例如,如果我有一个: 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. 好吧,我已经在python上做到了,但是我很难理解如何在反应时做到这一点。

That's how it looks on python for me. 这就是我在python上的样子。 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) 我不要求您为我编写代码(但是,如果您这样做: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 使用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'
]

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

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