简体   繁体   English

JavaScript嵌套的for循环可将值添加到对象

[英]JavaScript nested for-loop to add values to an object

I have encountered a situation in my code where I have three java script variables in which two are arrays and one is a single string variable. 我在代码中遇到了一种情况,我有三个Java脚本变量,其中两个是数组,一个是单个字符串变量。 The following are their structure: 以下是它们的结构:

var selectedUser = $('#Employees_SelectedValue').val(); //It has one one value "12121"
var selectedCountries = $('#Countries_SelectedValue').val();  //It has multiple values ["IND", "USA"]
var selectedSourceSystems = $('#SourceSystems_SelectedValue').val(); //It has multiple values ["SQL", "ORACLE", "MySQL"]

What I have to do is to add these values in a class on the basis of selectedUser such as User is same for all the values but the remaining two are different as: 我要做的是将这些值添加到基于selectedUser的类中,例如User的所有值都相同,但其余两个值不同,例如:

var userSettings = { userName: selectedUser, userCountry: selectedCountries, userSourceSystem: selectedSourceSystems };

The situation is to add the values from this class into an array in such a way that every userCountry and userSourceSystem will come as a single entity such as: 情况是将此类的值添加到数组中,以使每个userCountry和userSourceSystem都作为一个单独的实体出现,例如:

{ userName: "12121", userCountry: "IND", userSourceSystem: "SQL" },
{ userName: "12121", userCountry: "USA", userSourceSystem: "ORACLE" },
{ userName: "12121", userCountry: "", userSourceSystem: "MySQL" }

I'm trying the approach of nested-for loop to handle this scenario like: 我正在尝试使用嵌套循环的方法来处理这种情况,例如:

for (var i = 0; i < selectedCountries; i++)
        {
            for (var j = 0; j < selectedSourceSystems; j++)
            {
                userSettings.userName = selectedUser;
               //Add i and j values
            }
        }

Please suggest an effective approach other than this. 请提出除此以外的有效方法。

You may set up a 3×n matrix ( a 2d array) and rotate it by 90 degrees: 您可以设置3×n矩阵(二维数组)并将其旋转90度:

var matrix = [[selectedUser],selectedCountries,selectedSourceSystems];

var result =
   Array(//set up a new array
     matrix.reduce((l,row)=>Math.max(l,row.length),0)//get the longest row length
   ).fill(0)
   .map((_,x)=> matrix.map((row,i) => row[i?x:x%row.length] || ""));

Result 结果

If result should contain objects, then map the 2d array to objects: 如果结果应包含对象,则将2d数组映射到对象:

var objects = result.map(([a,b,c])=>({userName:a,userCountry:b,userSourceSystem:c})); 

result 结果

Small explanation: 小说明:

row[i?x:x%row.length] || ""

Actually does the following: 实际上执行以下操作:

If were in the first row ( i=0 ) ("12121")
  take whatever value of the array (x%row.length), so basically always "12121"
if not, try to get the value of the current column(x)
  if row[x] doesnt exist (||) take an empty string ("")

A more basic approach: 一个更基本的方法:

var result = [];
for(var i = 0,max = Math.max(selectedCountries.length,selectedSourceSystems.length);i<max;i++){

  result.push({
    userName:selectedUser,
    userCountry:selectedCountries[i]||"",
    userSourceSystem:selectedSourceSystems[i]||""
  });
}

result 结果

I believe it would be better to restructure your userSettings object in more natural way: 我相信以更自然的方式重组userSettings对象会更好:

userSettings: {
    name: "userName",
    countries: ["USA", "IND"],
    userSourceSystems: ["MySQL", "Oracle"]
}

Then you can fill it with settings from your inputs like this 然后,您可以像这样从输入中填充设置

for (item in selectedCountries)
    userSettings.countries.push(item)

for (item in selectedCountries)
    userSettings.userSourceSystems.push(item)

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

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