Skip to content Skip to sidebar Skip to footer

Replace With Previous String In A Json Array

I have the following array in a JSON file, is there a way to replace each 'TODO' with the entry above it. For example, the first 'TODO' should be 'Previous question' and the second

Solution 1:

Here's how I would do it :

// with ES6
myArray.filter(i => i.default === "TODO").forEach(i => i.default = i.englishDefault);

//without ES6for (var i=0; i<myArray.length; i++) {
    var cur = myArray[i];
    if (cur.default === "TODO") { cur.default = cur.englishDefault; }
}

Solution 2:

Improving @Aaron answer, you can use a for each and a ternary operator.

foreach (question in myArray) { question.default == "TODO" ? question.default = question.englishDefault : null; }

Post a Comment for "Replace With Previous String In A Json Array"