问题:我尚未声明的函数正在附加到从未分配过的变量的内存空间?
背景故事:我有一个通过引用比较对象键的值来删除重复对象的脚本。在初始化checkSum
变量和紧随其后的For...in loop
之后,将函数goodDiver的“ ghost”值添加到变量k
?
'use strict';
const goodDiver = require('good-diver');
// This package uses good-diver to make things simple :)
// Read more about it here: https://github.com/starcrusherproductions/good-diver
/**
*
* @param {object} object object you want to truncate
* @param {array} keysToCheck array of the key's path using good-diver
* @param {array} keyMustBe array consition of boolean values of what corresponding keysToCheck must be.
*/
function goodRemover(object, keysToCheck, keyMustBe = Array(keysToCheck.length).fill(true)) {
// Initialize c as a count to cycle through the filter later on
let c = 0;
// Pump the data into a reference variable
let reference = object.map(r => {
// Initialize an empty array to store the keys of duplicates
let keys = [];
// Create an iteratable data object call it iterator
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries
let iterator = object.entries();
// Iterate through the data object to find the keys
for (let i of iterator) {
// THIS IS WHERE THE PROBLEM IS?
// Create an array called checkSum. We will see if checkSum[0].length===checkSum[1].length
let checkSum = [keysToCheck,[]];
// Debug: Expected result: object [ 'teamName', 'teamId' ] 2
console.log(typeof(checkSum[0]), checkSum[0], checkSum[0].length);
// Returned Result: object [ 'teamName', 'teamId' ] 2
// For each keysToCheck in checkSum[0]
for(let k in keysToCheck) {
// Simplify set keysToCheck[k] to key for simplification
let key = keysToCheck[k];
// Debug expected result: string, k
console.log(typeof(key), k);
/**
* Returned:
* string 0
* string 1
* function goodDiver
*/
// ^^^ END OF PROBLEM???
// For object iteration does any of
if((i[1].goodDiver(key) === r.goodDiver(key)) === keyMustBe[k] ){
// Yes push a value to the checkSum
checkSum[1].push(true)
}
}
// Does object iteration match a schema?
if(checkSum[0].length===checkSum[1].length) {
// Yes push it's keys i[0] into the tracker
keys.push(i[0]);
}
}
// Return the object keys
return { keys }
})
// Now map through the reference object
.map(r => {
// Does object iteration of the reference have more than 1 key?
if(r.keys.length>1 && r.keys[0]===c) {
// Yes. Increment the counter and return just the first element
c++;
return r.keys[0];
}
// It does not
else {
// Increment c. Return nothing.
c++;
}
})
// The nature of map returns undefined if there is nothing to return
// We need to filter out the undefineds and just return stuff that isn't undefined
.filter(r => {
// Return boolean test
return r!=null;
});
/**
* Initialize a new array and iterate through reference to
* return data sets that match conditions in reference
*/
//
let newData = [];
//
for(let r in reference) {
newData.push(object[reference[r]]);
}
return newData;
}
let objectWithDuplicates = [
{
id: 1,
teamId: 1,
franchiseId: 2,
teamName: 'Gulls'
},
{
id: 2,
teamId: 1,
franchiseId: 2,
teamName: 'Gulls'
}]
goodRemover(objectWithDuplicates,['teamName','teamId']);
对goodDiver的引用是我的其他软件包之一,good-diver。如果删除依赖项,则不会将其自身分配给k。该
for... in loop
是唯一调用goodDiver
的地方。它没有分配任何值。它还没有以k的名称声明的变量。确实如此;但是,要处理对象原型。该条件语句是goodDiver唯一执行的地方。如果我评论说goodDiver的条件执行仍然附加到k上,那就更奇怪了吗?
最佳答案
我怀疑您的虚幻函数来自原型链(for..in循环遍历整个链)。您可以尝试使用Object.getOwnPropertyNames()
或Object.entries()
代替。