Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Project contains 7 cards , each card has own ID. I selected this ID .
I have data in JS file , this data has 7 objects with ID ,
I want compare card ID with data ID ,
IF THIS IDs WILL MATCH , THEN WILL PRINT IT .
console.log show me this
3 = card ID
(7) [1, 2, 3, 4, 5, 6, 7] = data file shows me array
How to change this array , I could compare card ID ?
const cardbody = document.querySelectorAll('.card')
cardbody.forEach(function(btn) {
console.log(btn) // it shows me current card
btn.addEventListener('click', function(e) {
const bt = e.currentTarget.dataset.id
console.log(bt) // 3
// data from JS file this return arrays
const dataid = cabins.map(function(item) {
return item.id
})
console.log(dataid) //(7) [1, 2, 3, 4, 5, 6, 7]
if(dataid === bt ) {
console.log('red')
}
})
})
If you just need to know if the bt is in any of the id properties, use the some() method. You don't need to create an array first.
cardbody.forEach(function(btn) {
console.log(btn) // it shows me current card
btn.addEventListener('click', function(e) {
const bt = e.currentTarget.dataset.id
console.log(bt) // 3
if (cabins.some(item => item.id == bt)) {
console.log('red')
}
})
})
const arr = [1, 2, 3, 4, 5, 6, 7];
const id = 3;
// existance
if (arr.includes(id)) {
console.log(`${id} exists`);
}
// findIndex
if (arr.indexOf(id) >= 0) {
console.log(`${id} found at arr[${arr.indexOf(id)}]`);
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
How can we split or divide an array into two new arrays?
SingleARR = [7,5,6,4,3,2,4,5,4,2,8,8];
one array should have values that don't repeat
and the other has values that repeat. Moreover, both new arrays should have different elements from each other.
First, count the frequencies. Then filter it by the frequency if it is one then that does not repeat and push it into one array. Then again filter it by the frequency, if it is greater than 1 then it repeats and pushes
let a = [7, 5, 6, 4, 3, 2, 4, 5, 4, 2, 8, 8];
let ret = a.reduce((p, c) => {
if (!p[c]) p[c] = 1;
else p[c] += 1;
return p;
}, {});
let x = [];
let y = [];
console.log(ret);
for (prop in ret) if (ret[prop] === 1) x.push(+prop);
for (prop in ret) if (ret[prop] > 1) y.push(+prop);
console.log(x);
console.log(y);
it into another array.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
In my code I have 6 lists of objects of different sizes.
I need to go through them all in a specific order, from the smallest list to the largest.
var list_1 = [...] // length 24
var list_2 = [...] // length 4
var list_3 = [...] // length 3
var list_4 = [...] // length 4
var list_5 = [...] // length 11
var list_6 = [...] // length 2
// Need code here for loop each list in order asc
list_6.forEach(...) // length 2
list_3.forEach(...) // length 3
list_2.forEach(...) // length 4
list_4.forEach(...) // length 4
list_5.forEach(...) // length 11
list_1.forEach(...) // length 24
Does anyone have a simple solution ? Thanks
You could add the lists in an array, sort it and perform the loop
[list, list2, ...]
.sort((a, b) => a.length - b.length)
.forEach(array => array.forEach(...))
Put the lists into another list and sort them.
const list1 = [1, 2, 3, 4],
list2 = [1],
list3 = [1, 2, 3, 4, 5, 6, 7];
let listOfLists = [list1, list2, list3].sort((a, b) => a.length - b.length);
console.log(listOfLists);
listOfLists.forEach(list => {
list.forEach(itemInList => {
console.log(itemInList);
});
});
See StackBlitz example.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have an array like this :-
let arr = [1,2,3,4,5,6,7,8,9]
I want to remove its last element after every 20 seconds and then add it in the beginning of its own array and this process will continuous and should never stop
Try
function arrayCycler(arr) {
const newArray = [...arr];
const lastElement = newArray.pop();
newArray.unshift(lastElement);
return newArray;
}
let newArray = [1,2,3,4,5,6,7,8,9]; // will be changed on every 20 seconds
setInterval(() => {
newArray = arrayCycler(newArray);
}, 20000);
Use setInterval to update the list every 20 seconds.
https://www.w3schools.com/jsref/met_win_setinterval.asp
You can use slice to get the array without the last element.
arr.slice(0, arr.length - 1)
I'll let you put it all together
In plain Javascript
var arr=[1,2,3,4,5,6,7];
let circularList=setInterval( function(){
arr.unshift(arr.pop())
console.log(arr)
if(arr.length==0)
clearInterval(circularList)
},20000);
Here's an example using an immediately-invoked function. Here it loops every second for convenience so adjust the counter from 1000 to 20000.
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
(function loop(arr) {
console.log(JSON.stringify(arr));
setTimeout(() => {
// `pop` off the last element
// and `unshift` on to the beginning
// of the array
arr.unshift(arr.pop());
// Call `loop` again with the
// updated array
loop(arr);
}, 1000);
}(arr));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a default 2 element array [0, 10], the expected behavior is that the first time a new array is presented, such as [2, 11], it will update into this new array [2, 11] since we no longer need the default, but for every subsequent new array presented, it will expand to min of the 0th element and max of the 1st element. Such as when passing [1, 9], the range will now be [1, 11]. And on passing [0, 12] next, it will now be [0, 12].
How can I translate this into code?
You could take a class for the wanted updateing by respecting the first update values.
class MinMax {
constructor (reference) {
this.first = true;
this.values = reference.slice(0, 2);
}
update (array) {
if (this.first) {
this.values = array.slice(0, 2);
this.first = false;
} else {
this.values[0] = Math.min(this.values[0], array[0]);
this.values[1] = Math.max(this.values[1], array[1]);
}
return this.values;
}
}
var minMax = new MinMax([0, 10]);
console.log(...minMax.values); // [0, 10]
minMax.update([2, 11])
console.log(...minMax.values); // [2, 11]
minMax.update([1, 9])
console.log(...minMax.values); // [1, 11]
minMax.update([0, 12])
console.log(...minMax.values); // [0, 12]
In the following f() takes two arrays and produces a new one with the minimum of index 0 and maximum of index 1. We can just nest this function manually or if we have an array of arrays where this function would have to be applied in the given order we can just use reduce in combination with our own function. Since I am unsure why you would need to keep track of the original array I simply sliced it off.
const f = (ar1, ar2) => [Math.min(ar1[0],ar2[0]),Math.max(ar1[1],ar2[1])]
console.log(f(f([2,11],[1,9]),[0,12]))
var arr = [[0,10],[2,11],[1,9],[0,12]];
let res = arr.slice(1).reduce(f);
console.log(res)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Assuming you have an array x = [1, 1, 2, 2, 2, 3], and you had to output a sorted array of objects containing a key-value pair (where the key represents the element, and the value the frequency) like:
y = [
{
2: 3
},
{
1: 2
},
{
3: 1
}
]
The resulting array should be sorted by the values.
What would be the best way of doing this?
You can create a temporary object and do simple .forEach and check if current number exists in object as key, if true plus 1 to the value, otherwise create that key, then with simple .map add all key value pairs in separate object in new array
const x = [1, 1, 2, 2, 2, 3];
const k = {};
x.forEach(v => {
if(k[v]) {
k[v] +=1;
} else {
k[v] = 1;
}
});
const y = Object.keys(k).sort((t,c) => k[c] - k[t]).map(key => ({[key]: k[key]}));
console.log(y);