0
var array1 = [column1,column2,column3,column4];
var array2 = [column1 [empid],column2 [mobno],column4 [place]];
  • if array1 has any of array2 values. I mean, in above case column1,column2,column4 is there in array1, then remove those values and array1 should have only column3 and then append array1 value with array2.
  • after check and remove, now array1 = [column3] then append array1 value with array2 and finally
  • array2 = [column1 [empid],column2 [mobno],column3,column4 [place]]; it should be in correct index position

is above case possible?

0

2 Answers 2

1

I would recommend using a library like Underscore.js, it has functions like _.union() that would do what you described. If you don't want to the library you could have a look at their implementation.

Using your example:

array2 = _.union(array1, array2);
Sign up to request clarification or add additional context in comments.

3 Comments

can you tell me how to use _.union. am getting error: _is not defined
@Rachel you need to download and import underscore.js into your project. It's not built in to the browser
@Rachel as I said in my answer, you could have a look in the underscore.js source to see what the _.union() function is doing.
0

if you dont want to use any library then you can do

for(elem in array2){

    var index = array1.indexOf(array2[elem]);
    if(index > -1)
    array1.splice(index,1);

}

array2 = array1.concat(array2).sort();

Comments

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.