1

Having some trouble using 2 arrays to form a new array of subarrays depending on whether the phones match up with the sims. I want to organise:

let phones = ["phone1", "phone2", "phone3"]

let sims = [ 'phone1sim1', 'phone1sim2', 'phone2sim1', 'phone2sim2', 'phone1sim3', 'phone3sim1' ]

into an array of subarrays like so:

let orgPhones = [

  ["phone1", ["phone1sim1", "phone1sim2"]],
  ["phone2", ["phone2sim1", "phone2sim2"]],
  ["phone3", ["phone3sim1"]]

]

any suggestions appreciated!!

2
  • 5
    Having some trouble using please post the code also, in which you're having trouble Commented Sep 19, 2019 at 9:42
  • 1
    Why are you using that format and not {"phone1": ["phone1sim1", "phone1sim2"],"phone2": ["phone2sim1", "phone2sim2"], "phone3": ["phone3sim1"]} Commented Sep 19, 2019 at 9:44

3 Answers 3

2

To return exactly what you require, you can utilise the Array.protoype methods; map and filter like so:

const organised = phones.map((phone)=> [phone, sims.filter(sim => sim.indexOf(phone)!== -1)]);

However, I would strongly encourage you to utilise a JavaScript object instead of an array, like so:

{
    phone1: ['phone1sim1', 'phone1sim2', 'phone1sim3']
    phone2: ['phone2sim1', 'phone2sim2']
    // etc...
}

Based on the anticipated result outlined above, I would use something along the lines of the following:

const phones = ['phone1', 'phone2', 'phone3'];
const sims = ['phone1sim1', 'phone1sim2', 'phone2sim1', 'phone2sim2', 'phone1sim3', 'phone3sim1'];

const organised = {};

phones.map((phone)=> organised[phone] = sims.filter(sim => sim.indexOf(phone)!== -1));
Sign up to request clarification or add additional context in comments.

1 Comment

awesome thank you. i just wanted arrays initially then will be converting them into a js object
2

You can use Map like this:

let phones = ["phone1", "phone2", "phone3"]

let sims = [ 'phone1sim1', 'phone1sim2', 'phone2sim1', 'phone2sim2', 'phone1sim3', 'phone3sim1' ]

let map = phones.reduce((map,phone)=>{
  return map.set(phone,sims.filter(sim=>sim.startsWith(phone)))
},new Map())

console.log(...map)

3 Comments

This is the best answer - although, I would still strongly suggest using an object instead of an array. +1
@Ben We have a Map that's more or less similar to object. We can use Map.get(key) to retrieve value array. 2D arrays can easily be converted to maps of keys/values and vice versa.
Oh yes!!!! I was focusing on the reduce and startsWith bits. This is a great answer!
0

you can iterate over phones array, and search for the related sim in sims array

let phones = ["phone1", "phone2", "phone3"]

let sims = ['phone1sim1', 'phone1sim2', 'phone2sim1', 'phone2sim2', 'phone1sim3', 'phone3sim1']

const res = phones.map((phone) => [phone, sims.filter(sim => sim.indexOf(phone) !== -1)])
console.log(res);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.