Json Object Array Inside Array Find and Replace in JavaScript

JSON Object array inside array find and replace in javascript

The following function will search through an object and all of its child objects/arrays, and replace the key with the new value. It will apply globally, so it won't stop after the first replacement. Uncomment the commented line to make it that way.

function findAndReplace(object, value, replacevalue) {
for (var x in object) {
if (object.hasOwnProperty(x)) {
if (typeof object[x] == 'object') {
findAndReplace(object[x], value, replacevalue);
}
if (object[x] == value) {
object["name"] = replacevalue;
// break; // uncomment to stop after first replacement
}
}
}
}

Working jsfiddle: http://jsfiddle.net/qLTB7/28/

Replace JSON object in array dynamically

You could use Array.prototype.splice()

In this example, the blue color of the index 2 will be replaced by the tomato color according to the value of the index variable.

const index = 2
const array = ['red', 'green', 'blue', 'orange']
const color = 'tomato'
const output = array.splice(index, 1, color)
console.log(array)

Is there any way to replace the existing object inside JSON array with new object based on key

Try this

let main_data = [{    client: [{            name: "aaaa",            count: "1",            filter: {                type: {                    name: "test3"                }            }        },        {            name: "bbbb",            count: "9",            filter: {                type: {                    name: "test2"                }            }        }    ]},{    compute: [{        name: "cccc",        count: "6",        filter: {            type: {                name: "test"            }        }    }]}];let new_data = [{client: [{    name: "bbbb",    count: "1234",    filter: {        type: {            name: "updated_name"        }    }}]}];
const res = main_data.map((item, index) => {if (item.client) { const clients = item.client.map(client => { if (client.name === new_data[0].client[0].name) { client = new_data[0].client[0]; } return client; }); return { client: clients };}return item;});
console.log(res);

Finding an item in a JSON object array with some values and replace if existing

Below is how your function addExternalBudget should look like. Your find method is incorrect, inner expression should not have curly braces. If it has curly braces, you will have to return the value explicitly -

addExternalBudget() {

let typeId:Number = this.selectedExternalBudget.TypeId;
let yr:number=this.selectedExternalBudget.Year;

if(typeId==0 || yr==0) return false;

let ebCopy: ExternalBudget = Object.assign({}, this.selectedExternalBudget);


let existingBudget:ExternalBudget=this.myChangeRequest.NewExternalBudget.find(x=> x.TypeId===typeId && x.Year==yr);
if(!existingBudget){
this.myChangeRequest.NewExternalBudget.push(ebCopy);
}
else
{
existingBudget.Cost=this.selectedExternalBudget.Cost+existingBudget.Cost;
}

}

Replacing an array by its length in nested JSON object using .map()

loop through the array of data and map the Object.entries of transactionTypes to have the lengths of its arrays :