I have a problem with converting an array to an Object in JS.
I received data from Back-end like this(below image) and I want to change detec_time array data to Object.
In detect_time data, the first element is "time" and the second element is "its value" ex) 0: (2) [18, 1794] is 18 o'clock and its value is 1794
Issue: Converting data.detect_time array to Object
What I expected : [ {10: its value} , { 11: its value }, ... {24: its value} ]
Attempt: 1) if statements --> add key & value in newObject called weekendDataObject floatingPopulationPerWeekendData.detect_time which means data.detect_time what I mentioned above
let weekendDataObject = {};
for (let i = 0; i < 25; i ) {
let timeIndex = 10;
if (
floatingPopulationPerWeekendData.detect_time &&
floatingPopulationPerWeekendData.detect_time[i][0] == timeIndex
)
return (weekendDataObject.timeIndex =
floatingPopulationPerWeekendData.detect_time &&
floatingPopulationPerWeekendData.detect_time[i][1]);
timeIndex ;
}
- making a new array of time and adding its value to new Object
const arr = [floatingPopulationPerWeekendData.detect_time &&
floatingPopulationPerWeekendData.detect_time[0][0]]
const dataObject = {
// floatingPopulationPerWeekendData.detect_time &&
// floatingPopulationPerWeekendData.detect_time[0][0] : floatingPopulationPerWeekendData.detect_time &&
// floatingPopulationPerWeekendData.detect_time[0][1]
arr[0] : floatingPopulationPerWeekendData.detect_time &&
floatingPopulationPerWeekendData.detect_time[0][1]
}
Both of them didn't work what I expected... Could you help me how to convert it to an Object? I really appreciate your help!
edit 1:
var response = floatingPopulationPerWeekendData;
var objects_from_array = response.detect_time.map(dt => {
return { time: dt[0], value: dt[1] };
});
console.log(response);
console.log(objects_from_array);
I assigned the responsibility as the data getting from the back-end and when I check the console. I checked the data as well. However, I got this error message "Cannot read properties of undefined (reading 'map')" So, I tried
var objects_from_array = ([] && response.detect_time).map(dt => {
return { time: dt[0], value: dt[1] };
});
and
var objects_from_array = (response && response.detect_time).map(dt => {
return { time: dt[0], value: dt[1] };
});
but both of them didn't work.
edit 2:
I tried to follow up another solution and here is what I trying to do
useEffect(() => {
fetch(`${BASE_URL}/notice/business/detected/${shopID}`, {
headers: {
"Content-Type": "application/json; charset=utf8",
Authorization: `Bearer ${localStorage.getItem("access")}`,
},
})
.then(res => res.json())
.then(data => {
if (data) {
let timeIndex = 10;
let weekendDataObject = {
timeIndex: "",
};
for (let i = 0; i < data.detect_time.length; i ) {
if (data.detect_time && data.detect_time[i][0] === timeIndex) {
weekendDataObject.timeIndex = data.detect_time[i][1];
console.log(weekendDataObject);
}
timeIndex ;
}
}
});
}, []);
When I trying to do like this I got this result
it should be time number instead of timeIndex string and I have to got 15 arrays but I only get a few of them. I guess my logic is something weird but I am not sure which part is wrong...
eidt 3: Adding what I expected the result ==> - What I expected : [ {10: its value} , { 11: its value }, ... {24: its value} ]
CodePudding user response:
So, mainly you need to add data to a new object like so:
let myObject = {}, timeIndex = 10;
var data = {
detect_time: [
[10,1794], [11,1503], [12, 1434], [13,1714], [14,503]
]
};
for (let i = 0; i < data.detect_time.length; i ) {
myObject[timeIndex] = data.detect_time[i][1];
timeIndex ;
}
console.log(myObject)
Suggestions
I can see some problems with your code, for instance you are declaring a variable inside the loop and then summing up this variable, which wont, because the loop is always declaring it as 10
at the start. It should be outside.
You can also make your code cleaner by changing your long variable names for smaller ones. For example floatingPopulationPerWeekendData
you could change it to data
. In your code would look like so:
let weekendDataObject = {};
for (let i = 0; i < 25; i ) {
let timeIndex = 10, data = floatingPopulationPerWeekendData;
if (
data.detect_time &&
data.detect_time[i][0] == timeIndex
)
return (weekendDataObject.timeIndex =
data.detect_time &&
data.detect_time[i][1]);
timeIndex ;
}
Also, if you are comparing variables is better to use ===
rather than ==
CodePudding user response:
Do you mean that you want to map every array inside detect_time to an object? Something like:
var detect_time_array_of_objects = backendResponse.detect_time.map(dt => {return {time: dt[0], value: dt[1]}});
the resulting structure will look like :
[
{
time: 18,
value: 1794
},
{
time: ....
value: ....
},
...
]
sorry I didn't test out the code. Here's a working snippet:
var response = { detect_time: [ [18,1794], [19,1503], [14, 1434]]};
var objects_from_array = response.detect_time.map(dt => {return {time: dt[0], value:dt[1]}});
console.log(objects_from_array);