1

I have a problem with picking some data with array of objects and pushing it into new object (in different array), but in different way.

const fruits = [
{name: "apple"},
{name: "kiwi"},
{name: "banana"},
{name: "orange"},
{name: "pineapple"},
{name: "coconut"},
{name: "peach"}, 
{name: "lemon"}
]

I want to pick a few items from this and paste them into completely new array, so it could look like this:

const manyFruits = [
{name: "apple-banana-kiwi-coconut"},
{name: "orange-pineapple-peach-lemon"}]

It would be great to do a function which takes an argument of how many fruits we want to pick into new array and they shouldn't repeat.

Below is my code. Firstly I create new array with names, then I push them into new array which depends on "length" and finally I try to create new array with objects, but it fails.

const fruitArray = length => {


const fruits = [
    {name: "apple"},
    {name: "kiwi"},
    {name: "banana"},
    {name: "orange"},
    {name: "pineapple"},
    {name: "coconut"},
    {name: "peach"},
    {name: "lemon"}
]

const allFruits = []

for (let i = 0; i < fruits.length; i++) {
    allFruits.push(fruits[i].name)
}

const newFruits =[]

for (let i = 0; i < length; i++) {
    newFruits.push(allFruits[i])
}

const manyFruitsInOneArr = []

for (let i = 0; i < 2; i++) {
    let newArr = {
        name: newFruits.join("-"),
    }

    manyFruitsInOneArr[i] = (newArr)
}

console.log(manyFruitsInOneArr)

}
fruitArray(2)

It generates new objects in new array, but items are the same all the time.

3
  • shouldnt repeat .. in one of the results or in all of them? So is apple-kiwi-banana and apple-kiwi-orange desired or not (for length 3)? Commented Feb 11, 2018 at 13:53
  • no, they shouldn't repeat at all. If we have apple-kiwi-banana, no apple, kiwi nor banana should appear again. Commented Feb 11, 2018 at 14:08
  • then ignore my answer... Commented Feb 11, 2018 at 14:09

2 Answers 2

2

You can create function for this using for loop and inside map() method to get array of names and join() to make a string from values.

const fruits = [{"name":"apple"},{"name":"kiwi"},{"name":"banana"},{"name":"orange"},{"name":"pineapple"},{"name":"coconut"},{"name":"peach"},{"name":"lemon"}]

const pick = (arr, n) => {
  const r = [];
  for (var i = 0; i < arr.length; i += n) {
    const name = arr
      .slice(i, i + n)
      .map(({name}) => name)
      .join('-');

    r.push({name})
  }
  return r;
}

const result = pick(fruits, 2)
console.log(result)

Update: to get names and sum of energy for each slice you can use map() and reduce() methods.

 fruits = [ {name: "apple", energy: 100}, {name: "kiwi", energy: 126}, {name: "banana", energy: 150}, {name: "orange", energy: 118}, {name: "pineapple", energy: 98}, {name: "coconut", energy: 83}, {name: "peach", energy: 65}, {name: "lemon", energy: 36} ] 

const pick = (arr, n) => {
  const r = [];
  for (var i = 0; i < arr.length; i += n) {
    const slice = arr.slice(i, i+n);
    const name = slice.map(({name}) => name).join('-')
    const energy = slice.reduce((r, {energy}) => r + energy, 0);
    r.push({name, energy})
  }
  return r;
}

const result = pick(fruits, 2)
console.log(result)

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! That really helped! I have one more question. If "fruits" looked like this const fruits = [ {name: "apple", energy: 100}, {name: "kiwi", energy: 126}, {name: "banana", energy: 150}, {name: "orange", energy: 118}, {name: "pineapple", energy: 98}, {name: "coconut", energy: 83}, {name: "peach", energy: 65}, {name: "lemon", energy: 36} ] how can I add "energy" position to the result, which sums up all energies from "used" fruits?
Sorry, I wasn't precise enough. The result should look like this: [ {name: "apple-kiwi", energy: 226}, {name: "banana-orange", energy: 268} ], where energy is a total from energy of apple and kiwi from "fruits"
Thanks! You are the best
Sorry to bother you again, but I have another problem. fruits database contains only 8 items, so if n = 3 the last item in result array is going to be "half-empty". For example n = 3, so the result is: {name: "apple-kiwi-banana", energy: 376}, {name: "orange-pineapple-coconut", energy: 299}, {name: "peach-lemon", energy: 101} As you can see peach-lemon has only 2 items. Items, which are not correct (doesn't contain n names) shouldn't be returned. I want the result to look like this: {name: "apple-kiwi-banana", energy: 376}, {name: "orange-pineapple-coconut", energy: 299}
And how to make an exception? For example I don't want apple to match with kiwi, so the position in the result should look like this: {name: "apple-banana-orange", energy: something}
|
1

That could be done using recursion:

 function subset(arr, n){
   if(!(n - 1)) return [arr[0].name];
   const result = [];
   for(let i = 0; i < arr.length - n; i++){
       for(const sub of subset(arr.slice(i + 1), n - 1)){
          result.push(arr[i].name + "-" + sub);
       }
   }
   return result;
 }

So you can do:

 subset(fruits, 5);

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.