how to change a value in an array of objects
What you are basically saying is:
filter this array to bring you only the items that the following action:
index "4" should be assigned with the opposite value
results into true, which means that the items that do not have index 4 (meaning its false in js) will be updated too.
What you need to do is:
console.log(indexStates.map(o => o[index] !== undefined ? {[index]: !o[index]} : o))
which will return a new array for you to use.
Find and replace value inside an array of objects javascript
You can use Array#find
.
let arr = [
{
"enabled": true,
"deviceID": "eI2K-6iUvVw:APA",
},
{
"enabled": true,
"deviceID": "e_Fhn7sWzXE:APA",
},
{
"enabled": true,
"deviceID": "e65K-6RRvVw:APA",
},
];
const id = 'eI2K-6iUvVw:APA';
arr.find(v => v.deviceID === id).enabled = false;
console.log(arr);
Can I update an array of objects by modifiying a value return by array.find()?
Find
just finds the first element in the array, they do not edit the element, but find the element. So when you have the element, you can edit afterward, but you do not have the index of the element inside the array, so you will need the index too if you want to pass the edited element in the array.
You have multiple ways to update. If you want a shorter way, right now it came into my mind like this :
setArray((oldState) =>
oldState.map((item) => {
if (item.value === "one") {
return {
value: "asdasd"
};
}
return {...item};
})
);
So directly you can set the value inside setArray
why? because the map returns a new array.
Or you can write more readable :
const newState = array.map((item) => {
if (item.value === "one") {
return {
value: "asdasd"
};
}
return {...item};
});
setArray(newState)
A similar question can be found here
If you have any questions ask. Thanks.
Change key's value in array of objects with javascript?
The variable myArray
is a array not a object, so the key
is just the index of the object in the array.
To check if the object with the specific key exists,
function getObjWithKey(myArray, key){
var retVal;
$.each(myArray, function(index, obj) {
if(key != undefined && obj[key]){
retVal = obj;
return false;
}
});
return retVal;
}
how to replace value id jquery with array key, after spice than change value id to array key
try this, I had not changed your logic of sorting, just added a map function to update the id
with the arr key
like
this.map((arr, i) => ({...arr, id:i}));
var each_arr = [{ id: 0, judul: "JUDUL 1" }, { id: 1, judul: "ICIK ICIK ehem" }, { id: 2, judul: "ASOLOLE" }, { id: 3, judul: "IWAK PEYEK" }];
$(".radio1").on('change', function(event, state) { var idarr = $(this).data("id"); each_arr = each_arr.move(idarr, 0); console.log(each_arr);});
Array.prototype.move = function(old_index, new_index) { if (new_index >= this.length) { var k = new_index - this.length; while ((k--) + 1) { this.push(undefined); } } this.splice(new_index, 0, this.splice(old_index, 1)[0]); return this.map((arr, i) => ({...arr, id:i}));};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><input type="radio" name="radio1" class="radio1" data-id="0">0<input type="radio" name="radio1" class="radio1" data-id="1">1<input type="radio" name="radio1" class="radio1" data-id="2">2<input type="radio" name="radio1" class="radio1" data-id="3">3
Related Topics
Javascript How to Create One Json by Combining Two Jsons
Dynamically Add Variable Name Value Pairs to Json Object
Retaining the Textbox Values Even After Refresh
How to Make One Observable Sequence Wait for Another to Complete Before Emitting
How to Bind on Click Event on Dynamically Created Button in Angular 6
Scroll to Element from Typescript Code (Angular 5)
Import Image from Json Source (Create-React-App)
Set State Twice in a Single Function - Reactjs
How to Convert All Elements in an Array to Integer in JavaScript
How to Import the Following Function in Another File
Stringified Json Has Unwanted Double Quotes
How to Display a Success Message After Redirecting the User Back to My Home Page
Add Two Numbers and Display Result in Textbox With JavaScript
How to Get Full Path of Selected File on Change of <Input Type='File'> Using Javascript, Jquery-Ajax
How to Remove All Null and Empty String Values from an Object
How to Read a Properties File in JavaScript from Project Directory