how to findIndex of [duplicate] - javascript

This question already has answers here:
How to find the array index with a value?
(12 answers)
Closed 5 years ago.
I'm wondering how to get the findindex of a specific service_name in the below array?
var obj = {"ID":111222,"NAME":"Chicken","FREQUENCY":"Yearly","service_name":["Open ticket","Service Account time","Assets Management Overview"]}
I have tried the following but not getting the correct index
var find_index = obj.service_name.findIndex(function(obj){return obj.service_name = "Open ticket"})
console.log ("The index of " + find_index);

Your code is close, but there are a couple of semantic issues. You're already iterating over the service_name array so the argument that the .findIndex callback takes is the array element / string itself. You also need to use == or === for comparison. Right now you are performing assignment.
const find_index = obj.service_name.findIndex(
serviceName => "Open ticket" === serviceName
);
In this case you can also use .indexOf which would be a bit simpler:
const index = obj.service_name.indexOf("Open ticket");

Related

How to manipulate an array, eliminating the empty items? [duplicate]

This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(142 answers)
How to remove item from array by value? [duplicate]
(37 answers)
Closed 2 months ago.
I have an array which has some empty items.
const array = ["a","","c","","e","f","g"]
I am trying to eliminate empty items and leave only the items having string. I mean manipulating the array as:
array = ["a","c","e","f","g"]
There are many alternative like array.map, array.filter, array.slice, array.splice.. What is the most costless and recommended way of doing this?
As far as I know most cost effective way is to use the array filter method:
const array = ["a","","c","","e","f","g"];
const results = array.filter(element => {
return element !== '';
});
console.log(results);

Javascript how to get non duplicates data from array? [duplicate]

This question already has answers here:
Finding items that appear only one time in a Javascript array
(5 answers)
Completely removing duplicate items from an array
(11 answers)
Closed 11 months ago.
let getNonDuplicates = [1,2,3,4,2,3,4,5]; // Here's my example array
the result I needed is to get 1 & 5 since those are the data that don't have duplicates
let resultMustBe = [1,5];
You could use filter method, to filter the array, based on the condition that index of an element from beginning is equal to index of the same element from the last. It means that the element is unique in the array.
let arr = [1,2,3,4,2,3,4,5];
let res = arr.filter(e=>arr.indexOf(e)===arr.lastIndexOf(e));
console.log(res);

how to select an index that has a specific name without case sensitivity? [duplicate]

This question already has answers here:
Access JavaScript property case-insensitively?
(19 answers)
Closed 2 years ago.
and I want to choose the index where the table is case insensitive
let table = {'tAble': [{'number': 1}],'CHair': [{'number': 2}]}
console.log(table[/table/i])
You can do this using Object.keys
table[Object.keys(table).find(x=> /table/i.test(x))]
Here you're getting an array of all keys, and then using find to test them with your RegExp, and then using the found key to retrieve the correct property of the object.
let index = Object.keys(table).findIndex(key => key.toLowerCase() === 'table');
let table = {'tAble': [{'number': 1}],'CHair': [{'number': 2}]}
let key = (Object.keys(table).find(i=>i.toLowerCase() === 'table'));
console.log(key)
console.log(table[key])

For...of es6 - how about getting index? [duplicate]

This question already has answers here:
How can the current number of i be accessed in a for of loop?
(6 answers)
Closed 6 years ago.
Lets say we have an array "myArray" and we want to iterate over it using the for..of. We are searching for a specific value and when we find it, we want to return the index where the value was. So, i have this:
var myArray=[1,2,3,4,5];
for (let item of myArray) {
if (item===3) {
//return index?
}
}
Is there any way to get the index? Thanks.
It does not come out of the box, but you may employ the new Array.prototype.entries(), which returns an iterator over the index-value pairs:
for (const [index, value] of myArray.entries()) {
// ...
}
Alternatively you could use the new Array.prototype.findIndex() which accepts a predicate and returns an index of the first element that matches it. Eg:
myArray.findIndex(v => v > 10); // would return an index of a first value
// that is greater than 10

Accessing dynamic content with numbers [duplicate]

This question already has answers here:
read name of unknown properties
(2 answers)
Closed 8 years ago.
I'm working with wikipedia API and have a problem with the results I'm getting.
{"query":{
"pages":{
"48636":{
"pageid":48636,
I don't know what the ID is going to be when I make the call, how do I access the 48636?
if gon.activateWiki
$(gon.keywords).each (index, element) ->
console.log element.name
$.getJSON( 'http://sv.wikipedia.org/w/api.php?action=query&format=json&prop=revisions%7Clinks%7Cimages%7Ccategories&rvprop=content&titles=' + element.name + '&callback=?' , (data) ->
console.log data.query.pages
)
You want the trusty old Object.keys
firstKey = Object.keys(data.query.pages).shift()
lastKey = Object.keys(data.query.pages).pop()
nthKey = Object.keys(data.query.pages)[n-1]
firstPage = data.query.pages[firstKey]
lastPage = data.query.pages[lastKey]
nthPage = data.query.pages[n-1]
You can access it like an array:
console.log(data.query.pages["48636"]);
Update:
To get values from unknown property's of object you need to use for..in statement. Or you can use slice method and transform your object to array:
var pages = Array.prototype.slice.call(data.query.pages);

Categories

Resources