2

How can I control the loop in reactjs? I just want that if the label or Group is equal it will not be displayed

generateAvailableOptions(data){
    const availableGroups = [];
    data.map(function(food){
        availableGroups.push({
                label: food.Group,
                options: [
                    { value: 'canAdd'+food.groupsid, label: food.description}
                ],
        })
    });
    this.setState({availableGroups: availableGroups})
}

The actual result

   Vegetables:
         Carrots
   Vegetables:
         Cabbage
   Vegetables:
         Potato
   Fruits:
         Mango
   Fruits:
         Apple
   Fruits:
         Pineapple

This is what I want results:

   Vegetables:
         Carrots
         Cabbage
         Potato
   Fruits:
         Mango
         Apple
         Pineapple
13
  • whta is structure of Vegetables: Carrots Vegetables: Cabbage Vegetables: Potato is it object? or array of object? Commented Jul 24, 2021 at 8:27
  • that is the result of data from the database Commented Jul 24, 2021 at 8:28
  • You mean it is json? Commented Jul 24, 2021 at 8:29
  • yes it is...... Commented Jul 24, 2021 at 8:29
  • 1
    Can you share value of data in its format. I mean the data you got form db Commented Jul 24, 2021 at 8:33

3 Answers 3

1

Here's the code:

const data = [
            {Group:'vegetable',description:'Carrots',groupsid:1},        
            {Group:'vegetable',description:'Cabbage',groupsid:1},
            {Group:'vegetable',description:'Potato',groupsid:1},       
            {Group:'fruit',description:'Mango',groupsid:2},        
            {Group:'fruit',description:'Apple',groupsid:2}
          ]
generateAvailableOptions(data){
    const result = data.reduce((item,curr)=> {
               if(!item[curr.Group]) item[curr.Group] = [];
                item[curr.Group].push({value: 'canAdd'+curr.groupsid, label: curr.description});
                return item;
            },{});
    
    const availableGroups = [];
    Object.entries(result).forEach(entry => {
      const [key, value] = entry;
        availableGroups.push({
        "label":key,
        "options": value
      })
    });
   this.setState({availableGroups: availableGroups});
}

You can see the result : https://es6console.com/krhjx0rz/

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

Comments

1

If I understand correctly you need to group by your array based on keys.

You can use Object.keys to get key of all object and then check to see if exists or not. if exists push new value if not assign to current array value.

So try this one:

var data = [{ "Vegetables": "Carrots" }, { "Vegetables": "Cabbage" }, { "Vegetables": "Potato", }, { "Fruits": "Mango" }, { "Fruits": "Apple" },{ "Fruits": "Pineapple" }]


var obj = {};

for (let a in data) {
    let keys = Object.keys(data[a]);
    for (var i = 0; i < keys.length; i++) {
        if (obj[keys[i]])
            obj[keys[i]].push(data[a][keys[i]])
        else
            obj[keys[i]] = [data[a][keys[i]]];
    }
    
}
console.log(obj);

Comments

0
var data = [
  { Group: "Veg", description: "potato", groupsid: 1 },
  { Group: "Veg", description: "cucumber", groupsid: 1 },
  { Group: "Fruit", description: "tomato", groupsid: 2 }
];
function generateAvailableOptions(data) {
  const distinctFoodObjs = [];
  // eslint-disable-next-line
  data.map((item) => {
    var findItem = distinctFoodObjs.find((x) => x.Group === item.Group);
    if (!findItem) distinctFoodObjs.push({ label: item.Group, options: [] });
  });
  // eslint-disable-next-line
  data.map((food) => {
    // eslint-disable-next-line
    distinctFoodObjs.map((arrElem) => {
      if (arrElem.label === food.Group) {
        arrElem.options.push({
          value: "canAdd" + food.groupsid,
          label: food.description
        });
      }
    });
  });
  console.log(distinctFoodObjs);
}
generateAvailableOptions(data);

Comments

Your Answer

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