Search array for specific value and find its position? - javascript

I have an array and I want to search for a specific value (that I know is in the array). I just need to return the position where this value is so later I can recall it with array[2] and that value will come up.
So if the array looks like:
11292929, 199309832092, 2198829922, 938430000113
If I search the array for 2198829922 I want to call the array later and use array[2] for 2198829922 to show up.
Thank you for help!

use the indexof() method on the array.
So it would be
var indexOfItem = arr.indexOf(2198829922);

Related

Get API array number by comparing ''Name''

I want to compare the name of a button I press with an array to find the matching name and then select that array number.
I currently have a For loop which gets all the info from every team in the array but I only need a specific one.
for( var i = 0 ; i <data.api.teams.length; i++)
.lenght needs to be the value corresponding to the team name I selected.
So for example: I press a button with the name and value of ''Twente'', I want the a for loop to search through the names to find ''Twente'' and return the number (''6'') as a value in order to use in another loop.
Does anyone know how I can handle this request?
The screenshot shows the API data I get from the call to get all the info I need.
To get array index of the item you can use Array.findIndex
teamIndex = data.api.teams.findIndex(
team => team.name == "Twente";
);
You're making this more complicated than you need to- you don't need a manual loop, just use Array.prototype.findIndex().
From MDN:
The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.
In your case, we'll pass findIndex() a function checking for the name match.
data.api.teams.findIndex(team => team.name == name);

find value in complex object javascript

Basically I have a complex object that retrieves the GPT API (google publisher tag) with this function:
googletag.pubads().getSlots();
The object value is something like this:
I need to know if there is a way to compare the value of each property with an X value without getting a problem of recursivity (because the object is huge and i need to to that validation several times)
Also, I tried to convert that object into a JSON with JSON.stringify(), and then tried to get the value with a regex, faster, but with this option, I have the problem with Cyclic Object Value.
Any suggestions ?
it's more simple. try it to convert it into an array and later use a filter for comparative with your value.
var objGoogle = {};
var arrayObjectGoogle = [objGoogle];
var filter = arrayObjectGoogle.filter(function(obj){
obj.yourAttr == yourValue; });
this will give you a second array with the values found it. later, index the array for pick up the value do you need.

In an Ember Array, how do i access object by index value? Ember Js

For an ember array you can simply do this:
array.get('firstObject');
to get the first object in array.
or this:
array.get('lastObject');
to get last object in array.
How do I get something by its index? similar to how it works in an ordinary javascript array:
array[index];
Looking at the documentation, you could just do var myObject = array.objectAt(someIndex);, and that will return the object at that specific index. You can check the documentation here.
array.get(index)
actually works. index can be either an integer or a string.
It also works for array-properties, ie.
this.get('myArray.1')
will return the second element in the myArray property.

Length of Array within Array

I have an object that I am pushing some ints, strings and arrays into an array. And I want to get the length of the array that is within said array.
This is my code
var all_categories = [];
all_categories.push({
title: theTitle,
id: theId,
sub: subcategories
});
Now I know that all_categories.length is the general way of getting the length and I believe that I can't run all_categories[0].sub[0].length will not work because the function does not exist.
Suggestions for a solution or work around?
In your statement all_categories[0].sub[0].length refers to the length of the first element of array named sub.
In order to see length of the array you should call:
all_categories[0].sub.length
Assuming that subcategories is the array you want the length of, take out the second [0]. You aren't trying to get the length of the first subcategory, you're trying to get the number of subcategories.

best way to append an item to a list within a JSON object?

I'm working on JavaScript and I have this JSON object:
var obj ={"name" : "objName",
"dynamicList" :[]};
and then I add some items to my list like this:
obj.dynamicList["someStringID"] = {"watever"};
It's important that I use a string as indexer for each item on my list (i didn't know this could be done until recently).
My only problem now is that whenever I ask for obj.dynamicList.lenght I get 0, unles I manually set the proper number... I was wondering if there's a better way to add items to my list?
Thanks!!
In Javascript, string index is not really an index. It's actually an attribute of the array object. You could set and get the value with the string index, but it's actually an empty array with some attributes. Not only .length, but also .sort(), .splice(), and other array function would not work. If there is a need to use array functions, I would use number as an index to make it a real item in the array.
If you have to use the string as an index, you couldn't rely on .length function. If there is no need to support IE prior to version 9, the Object.keys as suggested by #strimp099 should work. or you may have to create function to count the number of attributes for example:
function len(obj) {
var attrCount = 0;
for(var k in obj) {
attrCount++;
}
return attrCount;
}
and then call
len(obj.dynamicList);
Use the following the find the length of dynamicList object:
Object.keys(obj.dynamicList).length
To do this "the right way," you will have to make obj.dynamicList an object instead of an array; use {} instead of [] to set the initial value.
How to efficiently count the number of keys/properties of an object in JavaScript?
dynamiclist is an object, the length is not the length property you expect from an array.

Categories

Resources