Struggling with updating a value within an array, my research has not yielded a working solution. I have encountered multiple issues.
Array = [{
val1: "1",
val2: "2",
nameval: "name",
val4: "3"
},{
val1: "4",
val2: "5",
nameval: "name",
val4: "6"}]
function updateName(e) {
let pos = Array.findIndex(i => i.val1 === e.id );
if (Array[pos]) {
Array[pos][nameval] = "work";
} else {
Array[pos] = {
nameval: "work",
};
}
console.log(Array)
console.log("this should be 1 but it is: " + Array.findIndex(i => i.val1 === e.id))
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label>
<input onchange="updateName(this)" class="first-class" id ="4" >
</label>
Identified a potential issue in this code snippet Array.findIndex(i => i.val1
. It returns -1 indicating a failure.
Referencing helpful links: https://www.geeksforgeeks.org/indexof-method-in-an-object-array-in-javascript/
Resolved an error using the suggestions provided here: