Iterate Through Object Literal And Replace Strings
Can someone help me with the syntax for how to loop through an object literal using jQuery $.each and replace strings that match in each item? If I have: var foo = { item1: 'Ma
Solution 1:
Use String replace method to replace the text & update the value of the object
var foo = {
item1: "Mary had a little lamb",
item2: "Whose fleece was white as snow",
item3: "Johnny has a little lamb",
item4: "What's the deal with lambs?"
}
$.each(foo, function(i) {
foo[i] = foo[i].replace('lamb', 'lion')
});
console.log(foo)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Post a Comment for "Iterate Through Object Literal And Replace Strings"