determine location in a array [duplicate] - javascript

This question already has answers here:
How to find the array index with a value?
(12 answers)
Get the index of the object inside an array, matching a condition
(16 answers)
Closed 3 years ago.
Im trying to determine the location of a in a array. Im not really sure how to handle this case
const expect = require('chai').expect;
const answers = require('../src/arrays');
describe('arrays', function() {
let a;
beforeEach(function() {
a = [ 1, 2, 3, 4 ];
});
it('expect determine location of a in array', function(expect) {
expect(answers.indexOf(a, 3)).to.eql(2);
expect(answers.indexOf(a, 5)).to.eql(-1);
});

Try this!
var array = [ 1, 2, 3, 4 ];
function finding_index(element) {
return element === 2;
}
console.log(array.findIndex(finding_index));

Related

I need to reference the value of the same object on another value [duplicate]

This question already has answers here:
How can a JavaScript object refer to values in itself? [duplicate]
(8 answers)
Closed 1 year ago.
Hello i have an array of objects. Like this:
const obj = [{
count: 10,
value: count*2 // the previous count
}]
How can i reference 'count' on 'value' without having to find the index, or is a way to find the index of 'obj'?
You could take a getter with a reference to the same object.
const
objects = [{
count: 10,
get value () { return this.count * 2; }
}];
console.log(objects[0].value);

find first index of array in string javascript [duplicate]

This question already has answers here:
How do I check if an array includes a value in JavaScript?
(60 answers)
Closed 3 years ago.
There is a string variable that has a path like /app/something/xx/4/profile
besides there is an array of string like **
const arr=[
{name:'xx',etc...},
{name:'yy',etc...},
{name:'zz',etc...}
]
I want to find the first index of array that the string variable has the name in simplest way.
Use Array.findIndex which :
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.
const str = "/app/something/xx/4/profile";
const arr = [{ name: "xx" }, { name: "yy" }, { name: "zz" }];
const index = arr.findIndex(e => str.includes(e.name));
console.log({ index });

How to get most frequent/occurring element in an array [duplicate]

This question already has answers here:
Get the element with the highest occurrence in an array
(42 answers)
get most occurring elements in array JavaScript
(3 answers)
Frequent number in Javascript
(7 answers)
Closed 3 years ago.
I am currently being faced with this problem where I am trying to find the most occurring or frequent element in an array.
For example:
const arr = [1,2,1,1,1,2,2,2,1] (Most occuring is: 1)
Follow up: What if I want to get the second-most occurring element?
Here's my solution:
function returnMostOccurring(arr) {
const obj = {};
arr.forEach(item => {
if(!obj[item]) obj[item] = 1;
else obj[item]++;
})
const res = Object.entries(obj).sort((a,b) => b[1]-a[1]);
return res.shift();
}
I figured this is not the most optimal way to solve this problem (O(nLogN) time complexity)
What is the best way to handle this kind of use case?
You could take a single loop with a variable for the element with the actual max count and an object for keeping the counts of the elements.
function mostOccurringElement(array) {
var max = array[0],
counter = {},
i = array.length,
element;
while (i--) {
element = array[i];
if (!counter[element]) counter[element] = 0;
counter[element]++;
if (counter[max] < counter[element]) max = element;
}
return max;
}
const array = [1, 2, 1, 1, 1, 2, 2, 2, 1];
console.log(mostOccurringElement(array));

How to find the number of repetitions of an item in an array in JavaScript? [duplicate]

This question already has answers here:
Idiomatically find the number of occurrences a given value has in an array
(11 answers)
Count the number of times a same value appears in a javascript array
(11 answers)
Closed 3 years ago.
how can I find the times that 1 (for example) is repeated in this array?
myArray = [1, 2, 3, 1, 4, 5, 1, 6];
Try this solution
function getOccurrence(myArray, value) {
var count = 0;
myArray .forEach((val) => (val === value && count++));
return count;
}

Add an element to a multidimensional object? [duplicate]

This question already has answers here:
How can I add a key/value pair to a JavaScript object?
(26 answers)
Closed 6 years ago.
var John = { Cats: 2, Dogs: 3, Turtles: 1 };
var Mary = { Dogs: 0, Parakeets: 3};
How do I append new dimensions after I've already created the objects?
...John now also has 1 Parakeet
...Mary now also has 5 Koi
Just give them a value, like:
John.Parakeet = 1;
You can then later access these properties just like any other properties.
It really isn't hard.

Categories

Resources