J'ai deux tableaux comme ci-dessous:

var product1 = [
    {
      "Brand": "One Plus"
    },
    {
      "Brand": "One Plus"
    }
  ];
var product2 = [
    {
      "Brand": "One Plus"
    },
    {
      "Brand": "Apple"
    }
  ];

Je veux parcourir le tableau et imprimer ce qui suit:

  1. Si produit 1, sortie you have 2 One Plus
  2. Si produit 2, sortie you have 1 One Plus and 1 Apple

Voici le code que j'ai essayé.

var product1 = [
    {
      "Brand": "One Plus"
    },
    {
      "Brand": "One Plus"
    }
  ];
var product2 = [
    {
      "Brand": "One Plus"
    },
    {
      "Brand": "Apple"
    }
  ];

counter1 = {}
product1.forEach(function(obj) {
    var key = JSON.stringify(obj)
    counter1[key] = (counter1[key] || 0) + 1
});
console.log(counter1);
counter2 = {}
product2.forEach(function(obj) {
    var key = JSON.stringify(obj)
    counter2[key] = (counter2[key] || 0) + 1
});
console.log(counter2);

Je peux obtenir la sortie JSON, mais comment puis-je l'obtenir au format phrase?

2
user3872094 2 juin 2020 à 20:53

5 réponses

Meilleure réponse

Que dis-tu de ça?

var product1 = [{
    "Brand": "One Plus"
  },
  {
    "Brand": "One Plus"
  }
];
var product2 = [{
    "Brand": "One Plus"
  },
  {
    "Brand": "Apple"
  }
];

function countProducts(arr) {
  let counter = arr.reduce((acc, val) =>
    (acc[val.Brand] = (acc[val.Brand] || 0) + 1, acc), {});
  let strings = Object.keys(counter).map(k => `${counter[k]} ${k}`);
  return `You have ${strings.join(' and ')}`;
}

console.log(countProducts(product1));

console.log(countProducts(product2));
4
gyohza 2 juin 2020 à 18:43
const order1 = [
  {
    "Brand": "One Plus",
    "test" : "test"
  },
  {
    "Brand": "One Plus"
  },
  {
  	"Bar": "Foo"
  }
];
const order2 = [
  {
    "Brand": "One Plus"
  },
  {
    "Brand": "Apple"
  }
];

const orderToString = (order) => {
  if(order == null) return;
  
  const productsWithBrand = order.filter(product => Object.keys(product).includes('Brand'));
  const productBrandCounter = (counter, product) => (counter[product.Brand] = (counter[product.Brand] || 0) + 1, counter);
  const countPerProduct = productsWithBrand.reduce(productBrandCounter, {});
  const stringPerProduct = Object.keys(countPerProduct).map(brand => `${countPerProduct[brand]} ${brand}`);

  return `You have ${stringPerProduct.join(' and ')}`;
}

console.log(orderToString(order1));
0
Jens Ingels 2 juin 2020 à 21:01

Vous devez d'abord compter les occurrences, plutôt qu'une question de chaîne concat ou de modèles

function strinify(a) {
  let occurances = {};
  
  a.forEach(function(e,i){
   if( e.Brand in occurances ) {
    occurances[e.Brand] ++;
   } else {
    occurances[e.Brand] = 1;
   }
  });
   
  let str = [];
  
  for( brand in occurances ){
    count = occurances[brand];
    str.push(count + " " + brand);
  }; 
  
  return "you have " + str.join(" and ");
}
  
console.log(strinify([
    {
      "Brand": "One Plus"
    },
    {
      "Brand": "One Plus"
    },
    {
      "Brand": "Apple"
    }
]));
0
on8tom 2 juin 2020 à 18:16
const product1 = [
  {Brand: "One Plus"},
  {Brand: "One Plus"},
];
const product2 = [
  {Brand: "One Plus"},
  {Brand: "Apple"},
];

function whatYouHaveIn(list) {
  return `You have ` + Object.entries(list.reduce((a, c) => {
    a[c.Brand] = a[c.Brand] || 0;
    a[c.Brand]++;
    return a;
  }, {})).map(([brand, count]) => `${count} ${brand}`).join(` and `);
}

console.log(whatYouHaveIn(product1));

console.log(whatYouHaveIn(product2));
1
Chase 2 juin 2020 à 18:13
const product1s = product1.reduce((acc, product) => {
acc[product.Brand] = (acc[product.Brand] || 0) + 1;
return acc;
}, {});

console.log(
`You have ${
Object.keys(product1s).map(product => `${product1s[product]} ${product}`).join(" and ")
}`
);
1
user4642212 2 juin 2020 à 18:14