Encountering an issue while trying to insert an object, populated with values from an array, into another array. The problem arises during the second array.map function. At this point, arrValues ends up containing the current object instead of the one I intended to create in the first place.
Feeling puzzled!
let arrMap = [{
"field_desc": "Building Name",
"type": "string"
}]
let objFind = {
values: [
["BUILDING1", "BUILDING2"],
[]
]
}
let arrValues = [];
let objValues = {};
objValues.row = 0;
objValues.values = arrMap.map(function(element, index, array) {
let objFindExcel = objFind.values[0][0];
element.field_value = objFindExcel;
return element;
}, []);
arrValues.push(objValues);
objValues = {};
objValues.row = 1;
objValues.values = arrMap.map(function(element, index, array) {
let objFindExcel = objFind.values[0][1];
element.field_value = objFindExcel;
return element;
}, []);
arrValues.push(objValues);
console.log(arrValues);
Expecting the following output:
[{
"row": 0,
"values": [{
"field_desc": "Building Name",
"type": "string",
"field_value": "BUILDING1"
}]
}, {
"row": 1,
"values": [{
"field_desc": "Building Name",
"type": "string",
"field_value": "BUILDING2"
}]
}]
Instead, getting the following outcome:
[{
"row": 0,
"values": [{
"field_desc": "Building Name",
"type": "string",
"field_value": "BUILDING2"
}]
}, {
"row": 1,
"values": [{
"field_desc": "Building Name",
"type": "string",
"field_value": "BUILDING2"
}]
}]