I'm trying to run this function in my server.js file:
function formatArr (targetArr, characteristic, formattedArr) {
console.log(characteristic);
for (var i = 0; i < targetArr.length; i++) {
formattedArr.push({characteristic:targetArr[i]})
};
return formattedArr;
};
If I call it like so:
var targetSize = Object.keys(JSON.parse(req.query.size)); //[s,m,l]
var sizeKey = "size";
// format size array for mongodb query
var formattedSize = [];
var formattedSize = formatArr(targetSize, sizeKey, formattedSize);
console.log(formattedSize);
it DOES console log "size", but it does NOT replace the word characteristic with the word size in the formattedSize array. Here's what I get in my server console:
size
[ { characteristic: 's' },{ characteristic: 'm' },{ characteristic: 'l' } ]
How do I make it replace characteristic with size within the array? This is my desired output:
size
[ { size: 's' },{ size: 'm' },{ size: 'l' } ]
I want to be able to reuse the formatArr function with other characteristics.