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:
var array1 = ["JOHN", "JACK", "JACK"];var array2 = [{count: 9, value: "JACK"}, {count: 9, value: "JOHN"}, {count: 2, value: "TEST"}] var result = array2.map(({value, count}) => ({value, count: count - array1.filter(a=>a===value).length}))console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Related Topics
Charts.Js Graph Not Scaling to Canvas Size
How to Split Date and Time from the Datetime Variable in JavaScript
How to Prevent iOS Keyboard from Pushing the View Off Screen With CSS or Js
How to Remove the X-Axis from a Bar Chart Produced by Google'S Visualization API
Export Array of Objects into Excel Using JavaScript
Disable Pdf Download and Save Option
Change HTML Code Without Refreshing the Page
Onchange Event Not Fire When the Change Come from Another Function
Jquery Datatables: Hide Search Bar
How to Clear All Input Fields in Bootstrap Modal When Clicking Data-Dismiss Button
How to Remove Time from Datetime in SQL
Search Bar With Dropdown Results
Reactjs, Table Cell With Different Background Color
In Nest.Js, How to Get a Service Instance Inside a Decorator
Discord.Js Sending Message to Specific Channel
Uploading a File from a Bootstrap Modal Using Ajax
How to Find If a Text Contains Url String
How to Add Query String Parameter to Same Url When on Click a Href Link