Javascript 函数 - 对象循环中数组中对象的参数匹配

Javascript Function - Parameter match of Object in Array within an Object Loop

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);
}

结果是

Array [ "D School", "T School", "S School" ];
Array [ "D Safety" ];
Array [ "A Ind", "B Ind" ];

我想将其转换为可重复使用的函数。 即:

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.DataFile, catValueArray, 'Cat');

那是行不通的。但是,如果我在函数内部移动“Cat”,我可以得到一个结果。 即:

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);

最终结果是在一个数据集上重用,该数据集有超过 10 个将用作标识符的键,并且需要访问它们的项目数量非常大。目前,因为我无法获取传递给函数的 Object 参数,所以我不得不对每个需要访问密钥的实例进行硬编码。 IE:任何时候我需要从对象 dset2 中的数组数据文件中的对象访问“Cat”键,我必须用 dset2.DataFile[i].Cat.

明确地编写循环

将 属性 参数传递给函数的其他尝试均未成功。

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

我制作了一个函数,它可以执行您希望它执行的操作,并且只接受一个数组作为参数。

const array = [
    {
        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',
    },
];

const returnUniqueArrays = (arr) => {
    // Create a map to keep track of our arrays
    const arrayMap = {};

    // Iterate for every object in the dataset
    arr.forEach((obj) => {
        for (const [key, value] of Object.entries(obj)) {
            // If an array with that key doesn't already exist, add it and push the value
            if (!arrayMap[key]) {
                arrayMap[key] = [value];
            } else {
                // If the existing array doesn't have the unique value, push it
                arrayMap[key] = [...new Set([...arrayMap[key], value])];
            }
        }
    });

    return Object.values(arrayMap);
};

console.log(returnUniqueArrays(array));

你就快完成了!只需将 .prop 更改为 [prop],因此 if 中的部分将是:

    if (!array.includes(dataset[i][prop])) {
        array.push(dataset[i][prop]);
    }

演示版

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 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.DataFile, catValueArray, 'Cat');

函数可以简化为:

const pullUniqueValues = prop => [...new Set( 
    dset2.DataFile.map(file => file[prop]) 
)];

演示版

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",
        }
    ]
};

const pullUniqueValues = prop => [...new Set( dset2.DataFile.map(file => file[prop]) )];

console.log( pullUniqueValues('Cat') );
console.log( pullUniqueValues('Ind') );