Updating Array in Object Properties Using Property'S Name

updating array in object properties using property's name

const item = {Dogs:[{name: "lofi"}]}
let whichAnimal = Object.keys(item)[0]; //Dogs
let animalObj = item[whichAnimal][0]; //{name:'lofi'}

this.setState({
items: this.state.items[whichAnimal].push(animalObj)
})

You can use Object.keys to get all the keys and assuming your item has only one variable at a time, we set with index 0. Storing it in a variable so that during setState we will know which array in exact we going to push the item into

Change property name from an array of object javascript/typescript

You could handle it with a forEach:

myArray.forEach(item => {
let tmp = item.key;
item.key = item.name;
item.name = tmp;
});

It can technically work with map as well, but in general map should be used when you want to create a new array, and not to update in-place.

let newArray = myArray.map(item => {
return {
key: item.name,
name: item.key,
};
});

How to replace array of object property name in javascript

You can create a new array with the use of .map() and some Object Destructuring:

const data = [{name:"poran",id:"22"}];
const result = data.map(({ name:value , id:age }) => ({value, age}));
console.log(result);

Problem with updating object property in array of objects in React

I believe your categories start life as an array of category objects, and you meant to setCategories to an array - but instead you're setting it to an object.

You also need to make sure you dont double up tempCategory. something like this:

setCategories([
...categories.filter(x => x.id != tempCategory.id),
tempCategory
]);

However, this will reorder your categories, putting tempCategory at the end, so another option is to slice the array correctly

const tmpIdx = categories.findIndex(x => x.id == tempCategory.id);
setCategories([
...categories.slice(0,tmpIdx),
tempCategory,
...categories.slice(tmpIdx+1),
]);

Another option is make your categories an object and use Object.values(categories).map(...) but this makes ordering your categories harder, as there is no fixed ordering of object keys/values like there is with an array.

Update properties of an object of an array

Given: "I am getting the array of object by making .find call against id and there will be only one customer object in the received array"

// Given
let arr=[{"_id":"1234","customer":{"firstName":"John","lastName":"Doe","email":"johndoe@gmail.com","address":"123 Caroline Street"}}];
let ctx={"params":{"changeObject":{"lastName":"Ali"}}};

arr[0].customer = Object.assign(arr[0].customer, ctx.params.changeObject);

console.log(arr);

update a property in object array

You can get the result using array .map() and .filter() methods, assuming you are not allowed to change the original array: