How do I use forEach to loop through this [duplicate] - javascript

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 2 years ago.
let arr = [{
question:{
q1 : 123,
q2 : 44
}
}];
I've tried using forEach loop to do it, but it returns nothing.

If it's an array then you can do either map or forEach.
arr.forEach()
However you have to pay attention to the internal structure of the item as well, ex. in your case
arr.forEach(item => {
console.log(item.question.q1)
})
And if you want the transformation, then
const transformedArr = arr.map(item => item.question.q1)

Related

How to fill multidimensional array with empty arrays [duplicate]

This question already has answers here:
Unexpected behavior using Array Map on an Array Initialized with Array Fill [duplicate]
(1 answer)
Array.fill(Array) creates copies by references not by value [duplicate]
(3 answers)
Closed 25 days ago.
I am trying to initialize a two-dimensional array with empty arrays so I can add elements to them in a larger composition using Array.push. However, when I add to the inner arrays, they all get added to. Here is a simple example:
const arr = Array(3).fill([]);
arr[0].push(42);
Now arr is [[42],[42],[42]] but I was hoping for [[42],[],[]].
I think the problem is Array.fill is putting the same referenced empty array into each slot. How do I get fill to make a distinct empty array at each slot?
You can use Array#map.
const arr = [...Array(3)].map(_ => []);
arr[0].push(42);
console.log(arr);
Or Array.from.
const arr = Array.from({length: 3}, _ => []);
arr[0].push(42);
console.log(arr);

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);

how to get values in dropdown from print multiple arrays from an array in javascript or typescript? [duplicate]

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Closed 3 months ago.
let's see I have an array like let filterData= [[1,2,3],[x,y],[z,10]] Have to make it in single line or in single array as an output: this.filterData= [ 1,2,3,x,y,z,10];how can I achieve this in Javascript or typescript language.
anyone have a solution of it..please suggest
`if (this.data && this.data.length > 0) {
this.filteredData.push(this.data[index].value);
this.filteredData.push(this.filteredData);
console.log(this.filteredData)
}`
Just using Array.flat() can do it
let filterData= [[1,2,3],['x','y'],['z',10]]
filterData = filterData.flat()
console.log(filterData)
Try like this
let filterData= [[1,2,3],["x","y"],["z",10]];
let array=[];
filterData.map((x)=>{x.forEach((m)=>{array.push(m)});})
console.log(array);

The fastest and most efficient way to get repeated numbers in a nested array [duplicate]

This question already has answers here:
How to flatten nested array in javascript? [duplicate]
(27 answers)
Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
(97 answers)
Closed 5 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I know how to do this combining a lot of if...else statements but I need a faster and more efficient way.
I need a function that will run through a nested array and return the numbers that occur more than once in the nested array.
this doesn't work with nested arrays and even if you use flat(), the code still breaks when the duplicates in the array are more than 2
For example-
Lets call the name of the function deepSort(nestedArray)
where nestedArray is the nested array parameter
deepSort([[1,3,4,5], [4,7,9,1,3], [2,3,5], [1,2,3,4]]) //Returns 1,2,3,4,5
deepSort([[1,2,3], [4,5], [6,7,8], [2,9,0]]) //Returns 2
deepSort([[2,7,9], [4,3], [9,6,5], [1,4,3]]) //Returns 3,4,9
What I tried
function deepSort(nestedArray) {
const flatArr = nestedArray.flat().sort();
let results = []
for (let i = 0; i < flatArr.length - 1; i++) {
if (flatArr[i + 1] ==flatArr[i]) {
results.push(flatArr[i]);
}
}
return (results.filter((item,index) => results.indexOf(item) === index)).join()
}
Can this be optimized any more for speed and efficiency when handling larger values of data?

javascript how to filter various items in array [duplicate]

This question already has answers here:
remove objects from array by object property
(15 answers)
Filter array of objects based on another array in javascript
(10 answers)
How to remove objects from an array which match another array of ids
(4 answers)
Closed 10 months ago.
I need a function that filter some items from array ,like below:
how to make it? Thank you so much in advance !
const users = [
{id:"1",name:"Jane"},
{id:"2",name:"Lucy"},
{id:"3",name:"Li Li"},
{id:"4",name:"Hilton"},
{id:"5",name:"Rose"},
{id:"6",name:"Ha Ha"},
]
//user with this id need to been remove.
const filteredUsers = [
'1','2','5'
]
function filterUsers(allUsers,filteredUsers){
//return after filtered list
}

Categories

Resources