+ 3

how can i access a array from a nested array and convert them to string

var mobiles = [ { name: "IPhone 11", brand: "iPhone", camera: [32, 64], processor: ["snapdragon", "Qualcomm"], }, { name: "S20", brand: "Samsung", camera: [64, 128], processor: ["snapdragon", "Qualcomm", "exons"], }, { name: "Redmi k30", brand: "Redmi", camera: [32, 64], processor: ["snapdragon", "Qualcomm"], }, { name: "Redmi k20", brand: "Redmi", camera: [32, 64], processor: ["snapdragon", "Qualcomm"], }, ]; this is my array of object i need to access only the camera section of array and get those values and add them on a new array without any duplication (*only javascript no ES)

6th Aug 2021, 5:27 AM
officially for learning
15 Respuestas
+ 5
Officially for learning, Can you check whether this works? (Edit) function union( arr ) { var tempset = new Set(); // outer loop iterates through <arr> for ( var i = 0; i < arr.length; i++) { // forEach iterates through the arr[i].camera array // + https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/array/foreach arr[ i ].camera.forEach( function( el ) { // add element if <tempset> doesn't have it yet if( !tempset.has( el ) ) tempset.add( el ); } ); } // create array from Set <tempset> // then glue all its values as a string // + https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/array/from // + https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/array/join console.log( Array.from( tempset.values() ).join() ); } union( mobiles );
6th Aug 2021, 12:26 PM
Ipang
+ 2
const resolutions = [...new Set(mobiles.map(mobile => mobile.camera).flat())].join(","); https://code.sololearn.com/cfxeWpAf1xSf/?ref=app
6th Aug 2021, 2:05 PM
Calviղ
Calviղ - avatar
+ 1
Summing up on the title, code & Description. 1. <mobiles> is an array of object, not nested array (array of array). 2. You said you want to collect the values of 'camera' property, and they should be unique. Given that, am I correct to assume [[32, 64], [64, 128]] as the unique result? 3. Related to #2, what is the need to convert the unique results into string?
6th Aug 2021, 8:30 AM
Ipang
+ 1
Why the output is [ 32, 64, 128 ]? what information is this? I thought you wanted to collect unique camera resolutions, why the data gets mixed up in the end? Can you explain to me what is the purpose of that information?
6th Aug 2021, 9:54 AM
Ipang
+ 1
Officially for learning, It would be nice to see your latest version of the code that we can use to verify the issue, provided the steps to reproduce. Save it as code bit and share its link here https://www.sololearn.com/post/75089/?ref=app https://www.sololearn.com/Discuss/2852542/?ref=app
7th Aug 2021, 6:41 AM
Ipang
+ 1
Officially for learning, There are a lot of issues with the code. But to answer why the 32 is redundant, it happens because you created a string array for the 'camera' field. Because the new record has a string "32" rather than 32 (number), the "32" is included in the Set object.
7th Aug 2021, 1:30 PM
Ipang
+ 1
Look at line 73, camera: tempcamera.split(" ") Here <tempcamera> is a string, applying split() method makes it an array of string like ["32"]. But actually it needs to be converted to array of numbers like [32]. When checking for redundant number, look at line 290 if (!tempset.has(el)) tempset.add(el); Here the Set object contains (32, 64, 128) - all are numbers. The validation fails because there is not "32" (string) in the set, and thus "32" is inserted into the Set.
7th Aug 2021, 1:39 PM
Ipang
+ 1
This will turn the elements in array of string into its integer form Line 73: camera: tempcamera.split(" ").map(function(s){return parseInt(s)}),
7th Aug 2021, 1:51 PM
Ipang
+ 1
I'm not sure how it can be done. If this was a SQL table then it's easy, but it's not ...
7th Aug 2021, 1:59 PM
Ipang
+ 1
How can I print that ... Print what? you are already using a table, what you want to print?
8th Aug 2021, 12:13 PM
Ipang
+ 1
Officially for learning, You can add an element in which you can display the 'camera' array content, type of element to use is your choice. You can, depending on the element type, adjust the element's text by assigning its .innerHTML, .innerText or .value property. I hope I understood you correctly ...
8th Aug 2021, 2:44 PM
Ipang
+ 1
@Ipang i printed the array on the ui output, thanks for your continuous help.....!
9th Aug 2021, 1:34 PM
officially for learning
0
This is a 3-D array. Try the array bit and use the indexes. I may not explain because u may not understand 3-D arrays
8th Aug 2021, 1:23 PM
Mirembe Matia
Mirembe Matia - avatar