I'm new to the JavaScript. Here, I have an array which is like:-
var selected_Text_Include_Array = [{ annotation_Type:'tota',value:'abc', }]
And it has multiple such objects. Now, Here what I want is to get the id of that specific element.
for (var j = 0; j <= selected_Text_Include_Array.length - 1; j++) {
if (selected_Text_Include_Array[j].annotation_Type !== "undr") {
var index = getIndex(selected_Text_Include_Array, annotationName);
console.log("index is ==>", index);
if (index !== undefined) {
selected_Text_Include_Array.splice(index, 1);
}
}
}
Now, Here for getIndex method ,
var getIndex = function (array, annotationName) {
if (annotationName === "Address") {
var index = array.findIndex(function (x) {
return x.annotation_Type === "foot";
})
}
else if (annotationName === "FootNote") {
var index = array.findIndex(function (x) {
return x.annotation_Type === "addr";
})
}
else if (annotationName === "Overview") {
var index = array.findIndex(function (x) {
return x.annotation_Type === "tota";
})
}
else if (annotationName === "TotalExperience") {
var index = array.findIndex(function (x) {
return x.annotation_Type === "over";
})
};
}
So, with this, the condition gets matches but still it returns the id of that element undefined , I'm not getting why this is happening.
Can any one please help me with this?
I didnt understand very well what you are trying to accomplish but you are not returning the index value form your method.
var getIndex = function (array, annotationName) {
var index;
//conditions here and set your index
return index;
}
I didn't completely understand your code. from your code what I understand is the "annotationName" is not defined in the "for loop" and also you have to initialize the index as "-1" in getIndex function and then update index in each if condition then you have to return the index then only you will get a value. Sample code :
var getIndex = function (array, annotationName) {
var index =-1;
if (annotationName === "Address") {
var index = array.findIndex(function (x) {
return x.annotation_Type === "foot";
})
}
return index;
}
Change few things in your getIndex function, it will work fine, your code is almost correct, just a return statement missing
You are returning a array index value to "index variable"
and "function getIndex" not returning any value
//add the following statement to at the end of getIndex function before closing "}"
return index;
So your getIndex function becomes like this
var getIndex = function (array, annotationName) {
var index;
if (annotationName === "Address") {
index = array.findIndex(function (x) {
return x.annotation_Type === "foot";
})
}
else if (annotationName === "FootNote") {
index = array.findIndex(function (x) {
return x.annotation_Type === "addr";
})
}
else if (annotationName === "Overview") {
index = array.findIndex(function (x) {
return x.annotation_Type === "tota";
})
}
else if (annotationName === "TotalExperience") {
index = array.findIndex(function (x) {
return x.annotation_Type === "over";
})
};
return index;
}
Note: I am guessing you have array like this
var selected_Text_Include_Array = [{annotation_Type:"over"}, {annotation_Type:'foot'}];
JSBIN Example: https://jsbin.com/qifiday/edit?html,js,console,output
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.