var dset2 = {
"DataFile": [{
"ID": 1,
"Cat": "D School",
"Vert": "D Safety",
"Ind": "A Ind",
},
{
"ID": 2,
"Cat": "T School",
"Vert": "D Safety",
"Ind": "B Ind",
},
{
"ID": 3,
"Cat": "S School",
"Vert": "D Safety",
"Ind": "B Ind",
}
]}
var catValueArray = [];
var vertValueArray = [];
var indValueArray = [];
for (i = 0; i < dset2.DataFile.length; i++) {
if (!catValueArray.includes(dset2.DataFile[i].Cat)) {
catValueArray.push(dset2.DataFile[i].Cat);
}
if (!vertValueArray.includes(dset2.DataFile[i].Vert)) {
vertValueArray.push(dset2.DataFile[i].Vert);
}
if (!indValueArray.includes(dset2.DataFile[i].Ind)) {
indValueArray.push(dset2.DataFile[i].Ind);
}
console.log(catValueArray);
console.log(vertValueArray);
console.log(indValueArray);
}
Results are
Array [ "D School", "T School", "S School" ];
Array [ "D Safety" ];
Array [ "A Ind", "B Ind" ];
I would like to convert this into a reusable function.
IE:
var catValueArray = [];
var dsetSwitch = dset2.DataFile;
function pullUniqueValues(dataset, array, prop) {
for (i = 0; i < dataset.length; i++) {
if (!array.includes(dataset[i].prop)) {
array.push(dataset[i].prop);
}
}
console.log(array);
}
pullUniqueValues(dset2, catValueArray, Cat);
That doesn’t work. However, if I move "Cat" inside the function, I can get a result.
IE:
var catValueArray = [];
var dsetSwitch = dset2.DataFile;
function pullUniqueValues(dataset, array, prop) {
for (i = 0; i < dataset.length; i++) {
if (!array.includes(dataset[i].Cat)) {
array.push(dataset[i].Cat);
}
}
console.log(array);
}
pullUniqueValues(dsetSwitch, catValueArray);
The end result is to reuse on a a datasets that has over 10 keys that will be used as identifiers and the number of items ill need to access them is very large. Currently because I can not get the Object parameter passed into the function, I have been forced to hard code every instance where I need to access the key. IE: Anytime I need to access "Cat" key, from the Object in the Array DataFile from within the Object dset2, I have to explicerly write the loop with dset2.DataFile[i].Cat.
Additional attempts with no success passing the property parameter into the function.
var DataFile = [{
"ID": 1,
"Cat": "D School",
"Vert": "D Safety",
"Ind": "A Ind",
},
{
"ID": 2,
"Cat": "T School",
"Vert": "D Safety",
"Ind": "B Ind",
},
{
"ID": 3,
"Cat": "S School",
"Vert": "D Safety",
"Ind": "B Ind",
}
]
var uniqueValues = [];
function giveUniqueValues1(prop){
uniqueValues = DataFile.map(item => item.prop)
.filter((value, index, self) => self.indexOf(value) === index);
}
function giveUniqueValues2(prop){
uniqueValues = [...new Set(DataFile.map(item => item.prop))];
}
uniqueValues = DataFile.map(item => item.Cat)
.filter((value, index, self) => self.indexOf(value) === index); // Successs
giveUniqueValues1(Cat); //Failed Error
giveUniqueValues1('Cat'); //Failed Undefined result for uniqueValues;
uniqueValues = [...new Set(DataFile.map(item => item.Cat))]; //Success
giveUniqueValues2('Cat'); //Fail No Values Returned
giveUniqueValues2(Cat); //Fail Error Out
>Solution :
Just pass in the Object param as an argument, you will need to use bracket syntax to access properties off of a variable. Your code would look like this
function pullUniqueValues(dataset, array, prop) {
for (i = 0; i < dataset.length; i++) {
if (!array.includes(dataset[i][prop)) { //access prop via bracket here
array.push(dataset[i][prop); //and here
}
}
console.log(array);
}