3

I am trying to achieve that, fruits is checked in onTruck to find the existing keys, then push to a new array. Since they are different length, I am stuck on how to loop them.

var fruits = ['apple', 'orange', 'banana']

var onTruck = {
    'apple': 100,
    'orange': 200,
    'banana': 500,
    'pineapple': 5,
    'mango': 'empty'
    }

let arr = []

Object.keys(onTruck).forEach((e, i) => {
  console.log(e === fruit[]) <- Need to loop fruit  
  arr.push[...]
})

// OUTPUT: 
arr = [['apple', '100'],['orange', '200'], ['banana', '500'] ]

4 Answers 4

3

Use Array#map method to generate a new array based on the key element array.

var fruits = ['apple', 'orange', 'banana']

var onTruck = {
  'apple': 100,
  'orange': 200,
  'banana': 500,
  'pineapple': 5,
  'mango': 'empty'
}


var arr = fruits.map(v => [v, onTruck[v]]);

// in case some fields may miss in onTruck then add an additional filter
var arr1 = fruits.filter(v => v in onTruck).map(v => [v, onTruck[v]]);
// or with reduce
var arr2 = fruits.reduce((a, v) => v in onTruck ? [...a, [v, onTruck[v]]] : a, []);


console.log(arr)
console.log(arr1)
console.log(arr2)


FYI : If you want to ignore properties which is not number or value is empty then use an additional filter condition(or filter).

For avoiding non digit values : !isNaN(onTruck[v])

For avoiding property with value 'empty' : onTruck[v] !== 'empty'

For eg:

var arr = fruits.filter(v => v in onTruck && !isNaN(onTruck[v])).map(v => [v, onTruck[v]]);
Sign up to request clarification or add additional context in comments.

5 Comments

mango will also be a part of your array, which should not be.
var arr2 = fruits.reduce((a, v) => v in onTruck && !isNaN(onTruck[v]) ? [...a, [v, onTruck[v]]] : a, []);
@TAB : it's completely worked based on var fruits = ['apple', 'orange', 'banana']
Add mango in fruits. It will add empty fruit in resultant array.
@TAB : that's right mate, but we don't know the expected result... anyways I'd added it as side note in answer
1

You can simplify the code by using Array.filter and Array.map:

var fruits = ['apple', 'orange', 'banana'];
var onTruck = {
    'apple': 100,
    'orange': 200,
    'banana': 500,
    'pineapple': 5,
    'mango': 'empty'
}
const result = Object.keys(onTruck)
                     .filter((fruit) => fruits.includes(fruit))
                     .map((fruit) => [fruit, onTruck[fruit]]);
console.log(result);

Comments

0
var newArray = fruits.filter(function(fruit) {
  return fruit in onTruck;
}).map(function(fruit) {
  return [fruit, onTruck[fruit]]
});

Should work. Basically, I am filtering fruits existing in onTruck and then constructing the required array through the map function.

Comments

0
var fruits = ['apple', 'orange', 'banana']

var onTruck = {
    'apple': 100,
    'orange': 200,
    'banana': 500,
    'pineapple': 5,
        'mango': 'empty'
    }

let arr = fruits.reduce((arr,b) => {
    if (!isNaN(onTruck[b])){
        arr.push([b, onTruck[b]]);
        return arr;
    } else return arr;

    },[])



console.log(arr)

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.