How to Change Value of Object Which Is Inside an Array Using JavaScript or Jquery

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



Leave a reply



Submit