0

For example this the json which i get.Json group this

var data = [{
        orderid: 1,
        ordername:ordername1,
        productcode: 1,
        productname: product1,
        productquantity: 10
    },
    {
        orderid: 2,
        ordername:ordername2,
        productcode: 2,
        productname: product2,
        productquantity: 11
    },
    {
        orderid: 1,
        ordername:ordername3,
        productcode: 3,
        productname: product3,
        productquantiy: 45
    }
]

Expected output

[
{
    orderid: 1,
    ordername:ordername1,
    products: [
    {
        productcode: 1,
        productname: product1,
        productquantity: 10
    },
    {
        productcode: 3,
        productname: product3,
        productquantity: 45
    }]
},
{
    orderid: 2,
    ordername:ordername2,
    products: [
    {
        productid: 2,
        productname: product2,
        productquantity: 11
    }]
}]   

How to do this in react native.How to group this based on orderid.Now i have added ordername also so how to include that also

2
  • I'd use lodash for this Commented Oct 1, 2019 at 16:45
  • can you give an example Commented Oct 1, 2019 at 16:46

2 Answers 2

1

Easily achievable using Array.reduce

var data = [{
    orderid: 1,
    ordername: 'ordername1',
    productcode: 1,
    productname: 'product1',
    productquantity: 10
  },
  {
    orderid: 2,
    ordername: 'ordername2',
    productcode: 2,
    productname: 'product2',
    productquantity: 11
  },
  {
    orderid: 1,
    ordername: 'ordername3',
    productcode: 3,
    productname: 'product3',
    productquantity: 45
  }
];


// reduce iterates over each item, using accumulator
var orderedData = data.reduce((acc, next) => {

  // reusable product var
  var nextProduct = {
    productcode: next.productcode,
    productname: next.productname,
    productquantiy: next.productquantity
  }

  // find similar orders, and join them
  var exist = acc.find(v => v.orderid === next.orderid);
  if (exist) {

    // order exists, update its products
    exist.products.push(nextProduct);
  } else {

    // create new order
    acc.push({
      orderid: next.orderid,
      ordername: next.ordername,
      products: [nextProduct]
    })
  }
  return acc
}, [])

console.log(orderedData)

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

Comments

0

Tried implementing generically

function group(data,property,extra=[]){
	let output=[]
	data.forEach(item=>{
		const indexOfItem=output.length>0 ?output.findIndex(outputItem=>outputItem[property]===item[property]):-1
		if(indexOfItem===-1){
            let tempOutput={}
            extra.forEach(key=>{
             if(item[key]){tempOutput[key]=item[key]}
            })
			let {[property]:propertyValue,...rest}=item;
			output.push({[property]:propertyValue,...tempOutput,products:[{...rest}]})
		}
		else{
			let {[property]:propertyValue,...rest}=item;
			output[indexOfItem].products.push({...rest})
		}
	})
	return output
}

var data = [{
        orderid: 1,
        ordername:'order1',
        productcode: 1,
        productname: "product1",
        productquantity: 10
    },
    {
        orderid: 2,
        ordername:'order2',
        productcode: 2,
        productname: "product2",
        productquantity: 11
    },
    {
        orderid: 1,
        ordername:'order3',
        productcode: 3,
        productname: "product3",
        productquantiy: 45
    }
]

console.log(group(data,'orderid',['ordername']))
//console.log(group(data,'productcode'))

Now you can use group(data,"property name")

2 Comments

@ Thakur Karthik now i have added ordername also so how do i get that
@Annaz added additional modification,now you can pass the extra variables.But remember they must also be unique like id or else they will be overriden by later values.Try it out!

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.