简体   繁体   English

Javascript重写一个数组

[英]Javascript rewriting an array

I have two arrays that are dynamic which could change at any time. 我有两个动态的数组,可以随时改变。 For example: 例如:

var vehicleColor = "red,green,blue,white".split(",");
var vehicleType = "Truck,Car,Bus".split(",");

How can I incorporate these arrays to make a javascript array in this format for my graph. 如何将这些数组合并为我的图形以这种格式制作javascript数组。

var links = [
    {source: vehicleColor[0], target: vehicleType[0]},
    {source: vehicleColor[1], target: vehicleType[0]},  
    {source: vehicleColor[2], target: vehicleType[0]},
    {source: vehicleColor[3], target: vehicleType[0]},  
    {source: vehicleColor[0], target: vehicleType[1]},
    {source: vehicleColor[1], target: vehicleType[1]},
    etc...
]

Basically I need an array that loops through all of the elements in both arrays but creating a separate line for each one. 基本上我需要一个循环遍历两个数组中所有元素的数组,但为每个数组创建一个单独的行。

I've tried: 我试过了:

links = links.map(x => ({source:vehicleColor, target:vehicleType}));

but this doesn't get me each element in a separate line. 但这并没有让我把每个元素都放在一个单独的行中。

The map method gives me this output map方法给了我这个输出

var links = [
    {source: ["red","green","blue","white"], target: ["Truck","Bus","Car"]},
]

You'd use nested loops: 您将使用嵌套循环:

 var vehicleColor = "red,green,blue,white".split(","); var vehicleType = "Truck,Car,Bus".split(","); var links = []; for (var i = 0; i < vehicleType.length; ++i) { var type = vehicleType[i]; for (var j = 0; j < vehicleColor.length; ++j) { links.push({ source: type, target: vehicleColor[j] }); } } console.log(links); 

You could take a recursive function which separates all key/value pairs and build a new cartesian product by iterating the values, if an array with objects call getCartesian again and build new objects. 如果带有对象的数组再次调用getCartesian并构建新对象,则可以采用递归函数分隔所有键/值对并通过迭代值构建新的笛卡尔积。

 function getCartesian(object) { return Object.entries(object).reduce((r, [k, v]) => { var temp = []; r.forEach(s => (Array.isArray(v) ? v : [v]).forEach(w => (w && typeof w === 'object' ? getCartesian(w) : [w]).forEach(x => temp.push(Object.assign({}, s, { [k]: x })) ) ) ); return temp; }, [{}]); } var data = { source: ["red", "green", "blue", "white"], target: ["Truck", "Bus", "Car"] }; console.log(getCartesian(data)); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

I used a foreach and map to get the desired result 我使用foreach和map来获得所需的结果

 var vehicleColor = "red,green,blue,white".split(","); var vehicleType = "Truck,Car,Bus".split(","); let res = []; vehicleColor.forEach((color) => { res = res.concat(vehicleType.map((type) => { return {'source': color, 'target': type} })) }) console.log(res) 

You may run two run two loops. 你可以运行两个运行两个循环。 For example when taking each color as a constant run the complete loop for vehicle, then repeat the same for another color 例如,当将每种颜色作为常数运行时,运行车辆的完整循环,然后对另一种颜色重复相同

 var vehicleColor = "red,green,blue,white".split(","); var vehicleType = "Truck,Car,Bus".split(","); let links = []; vehicleColor.forEach(function(item) { vehicleType.forEach(function(ve) { links.push({ source: item, target: ve }) }) }) console.log(links) 

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

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