0

im new in reactjs, is that possible to do in reactjs,like mysql inner join, here my sample code.

          {dataCustomer.map((customer) => (
            <tr>
              <td>{customer.custid}</td>
              <td>{customer.custname}</td>
              <td>{customer.mailingaddr}</td>
              <td>{customer.stateid}</td>
              <td>{customer.districtid}</td>
              <td>{customer.postcode}</td>
            </tr>
          ))}

          {dataState.map((state) => (
            <tr>
              <td>{state.stateid}</td>
              <td>{state.statename}</td>
            </tr>
          ))}

how to join array map data from customer.stateid & state.stateid to call state.statename without use SQL syntax?..Is that possible?

0

2 Answers 2

1

This should work

let joinedData=[]
dataCustomer.forEach((customer)=>{
let st= dataState.filter((state)=>state.stateid===customer.stateid);
if(st.length>0)
joinedData.push({...customer,statename:st[0].statename})
})

joinedData array will now contain the customer objects with the corresponding statename and you can use it as

{joinedData.map((customer) => (
        <tr>
          <td>{customer.custid}</td>
          <td>{customer.custname}</td>
          <td>{customer.mailingaddr}</td>
          <td>{customer.stateid}</td>
          <td>{customer.districtid}</td>
          <td>{customer.postcode}</td>
          <td>{customer.statename}</td>
        </tr>
      ))}
Sign up to request clarification or add additional context in comments.

Comments

0

It is not exactly the same but I think the function below that I wrote can help you:

let customers = [{
    name: "Foo",
    stateid: 1
  },
  {
    name: "Boo",
    stateid: 3
  },
  {
    name: "Goo",
    stateid: 2
  },
  {
    name: "Zoo",
    stateid: 2
  },
];

let states = [{
    name: "State1",
    id: 1
  },
  {
    name: "State2",
    id: 2
  },
];

function join(customers, states, column1, column2) {
  return customers.map(customer => {
    states.forEach(state => {
      if (customer[column1] === state[column2]) {
        customer["state"] = { ...state };
      }
    })
    return customer;
  })
}

console.log(join(customers, states, "stateid", "id"))

Note that column1 and column2 parameters are the properties you are joining on.

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.