0

to clarify I'm using React, Node, Postgres, Express.

I have an express route that return a table row, where each element is a different column. I want to assign each of these elements to a variable, here is an example of how the return results will look like:

Object { test1_id: "4", parts_id: 26, parts_material: "Plexiglass", … }
​
parts_id: 26
​
parts_material: "Plexiglass"
​​
parts_material_length: "49.78"
​​
parts_material_thickness: "1.86"
​​
parts_material_width: "24.96"
​​
parts_produced: 5
​​
test1_id: "4"
​​
workorder_id: 2
​​
workorder_total: 76

And here is my attempt at mapping these elements to seperate variables:

let thickness1 = 0;
    let width1 = 0;
    let length1 = 0;
    let partNeeded = 0;

// Material requirements calculation
    const getReq = async (id) => {
        try {
            console.log(materials);

            const response = await fetch(`http://localhost:5000/materials/${id}`, [id])
            const jsonData = await response.json();

            jsonData.map(x => x.parts_material_thickness = thickness1);

            console.log(jsonData)
            console.log('ID is: ',id)
            //console.log();
            //console.log('Thickness is: ', thickness1);


        } catch (err) {
            console.log(err.message);
        }
    }

My goal here is that every row will have different results, and the variables thickness1, etc will have different values depending on the array that was returned. In the example I provided I only tried mapping 1 variable, but even that isn't working. So how would I go using map() to map my array elements to different variables?

6
  • Does this answer your question? Multiply specific object in array Commented Mar 25, 2022 at 18:57
  • No. map is not going to help you here, that works only on arrays. Commented Mar 25, 2022 at 18:58
  • "I have an express route that return a table row", "every row will have different results" - do you have one row (an object) or many (an array of objects)? It's not clear. Please post the JSON that your API returns. Commented Mar 25, 2022 at 18:59
  • you can check my answer below @test3r123 Commented Mar 25, 2022 at 19:03
  • @Bergi I included the JSON that will be returned. To clarify, I have a table with columns and I want to map the values for those column to individual variables. So for the sake of example if I get returned {1, 2, 3 , 4, 5}, I want to map it as: x = 1 y = 2 z = 3 f = 4... etc. Commented Mar 25, 2022 at 19:03

1 Answer 1

1

I think you can use .map() for this task, but not on the jsonData array itself, but on the array of the properties of your row object.

// This assumes that:
// 1. The order of values in jsonData matches
//    with the order of properties in the row object.
// 2. The row object already has properties (has columns)
//    but no values yet (undefined or null)
const assignedRowEntries = Object.entries(rowObject).map(
    [key, value], index => [key, jsonData[index]]
);

console.log(assignedRowEntries);
// assignedRowEntries should look like this:
// [
//    [parts_id, value1],
//    [parts_material, value2],
//    ...
//    [workorder_total, value3]
// ]

// This is what you needed
const assignedRowObject = Object.fromEntries(assignedRowEntries);

More about Object.fromEntries() and Object.entries().

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

1 Comment

TBH I gave up trying to get it to work with map(), using useState() instead works just fine. I'll have to figure out how to get it work with map() one day, thanks!

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.