0

First array

var danu = ["nu", "da", "nu", "da", "da", "da", "da", "da", "da", "nu", "nu"];

Second array

var abc2 = ["2", "3", "2", "3", "3", "2", "2"];

I want to replace all "da" from first array with the values from the second array 1 by 1 so i will get

["nu", "2", "nu", "3", "2", "3", "3", "2", "2", "nu", "nu"];

I want the fix only in Javascript. I tried using the loop metod

for (i=0; i<danu.length; i++) {
if (danu[i] == "da") {
 danu[i] = abc2[i];
}
}

But i end up getting the array to this

["nu", "3", "nu", "3", "3", "2", "2", undefined, undefined, "nu", "nu"]
4
  • 2
    So loop through and replace with the second... Commented Dec 11, 2015 at 21:02
  • I tried looping through but i don't know exactly how should i do to work. Commented Dec 11, 2015 at 21:06
  • Welcome to SO. Please visit the help center to see how and what to ask here. Hint: Use a for loop or some of the array prototype methods developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/… Commented Dec 11, 2015 at 21:08
  • if ( value equals ) Replace value with first value from other array, increment counter. Commented Dec 11, 2015 at 21:08

3 Answers 3

2

You can use .map to apply changes on each item in the danu array. Then have a c count which increments when "da" was found.

danu = ["nu", "da", "nu", "da", "da", "da", "da", "da", "da", "nu", "nu"];
abc2 = ["2", "3", "2", "3", "3", "2", "2"];

c = 0;

newArr = danu.map(function(e,i){
    if(e == 'da') return abc2[c++];
    return e;
});

console.log(newArr);
Sign up to request clarification or add additional context in comments.

3 Comments

Could you not start at 0 and return abc2[c++]; ?
@mplungjan That's a good point, I've added that into my answer. Thank you.
@mplungjan For the second method you will want to use shift rather than pop. jsfiddle.net/90jgjekd/4
1

Just use Array.prototype.reduce():

var danu = ["nu", "da", "nu", "da", "da", "da", "da", "da", "da", "nu", "nu"],
    abc2 = ["2", "3", "2", "3", "3", "2", "2"];
  
danu.reduce(function (r, a, i, o) {
    o[i] = a === 'da' ? abc2[r++] : a;
    return r;
}, 0);

document.write('<pre>' + JSON.stringify(danu, 0, 4) + '</pre>');

1 Comment

@mplungjan, yes, map is shorter for a new array, but this solution works in situ.
0

Assuming that the second array is equal in length to the number of "da" in the first array, the code would look like:

var j = 0;
for(int i; i<danu.length; i++){
    if(danu[i] === "da"){
        danu[i] = abc2[j];
        j = j + 1;
    }
}

However, if the second array is of different length, incrementing j could put abc2 out of bounds. You would need a different strategy based on what you are actually trying to accomplish with this.

1 Comment

Hm, it doesn't work. I get unexpected identifier error...but thanks anyway, i got the fix from previous answer.

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.