0

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. 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:

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:

{ 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.

5
  • 1
    Please at first differentiate class,object and array . youve mixed them up a bit... Commented Jul 31, 2017 at 11:28
  • 1
    you have made an object userSettings and then treating it like an array userSetting[0] . what are you trying to do. Commented Jul 31, 2017 at 11:29
  • 1
    This is not valid syntax -> {"12121", "IND", "SQL"}. error would be "Unexpected token ," Commented Jul 31, 2017 at 11:40
  • Sorry guys, fixed the content. Got a little confused between C# and Javascript but the question is to get the values of userCountry and userSourceSystem as single record with the userName. Commented Jul 31, 2017 at 12:27
  • 1
    @sahil sharma now it makes more sense. updated answer to fullfill your wanted structure. Commented Jul 31, 2017 at 14:57

2 Answers 2

1

You may set up a 3×n matrix ( a 2d array) and rotate it by 90 degrees:

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:

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

Sign up to request clarification or add additional context in comments.

1 Comment

That's what I'm looking for. Thanks a lot buddy. Like you have calculated the max length and iterate the loop until it is greater than 0, I was trying the same thing using nested for loops But this approach seems simpler and efficient. Cheers
0

I believe it would be better to restructure your userSettings object in more natural way:

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)

1 Comment

Theres no need to fill , you can simply put it in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.