问题:我没有声明的函数是附加到一个变量的内存空间,我从来没有分配给它?
背景:我构建了一个脚本,通过比较对象键的值来删除重复的对象。在初始化checkSum变量和紧接着的For...in loop之后,将函数goodDiver的“鬼”值添加到变量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的引用是我的其他包之一,好潜水员。如果我移除依赖项,它就不会将自己分配给k。这个for... in loop是唯一调用goodDiver的地方。它不被分配给任何值。它也没有以k的名字声明的变量,它是这样做的;但是,它是在对象原型上工作。
该条件语句是执行goodDiver的唯一位置。更奇怪的是,如果我评论说,有条件执行goodDiver仍然附加在k?
发布于 2018-11-03 20:19:58
我怀疑您的鬼函数来自一个原型链(for..in循环遍历整个链)。您可以尝试使用Object.getOwnPropertyNames()或Object.entries()。
发布于 2018-11-03 23:31:35
虽然这并不是对您的问题的直接回答,但是看起来您可以更容易地使用房客实现您想要的功能。get执行与您包含的库相同的操作,uniqWith执行筛选。例如:
let objectWithDuplicates = [
{
id: 1,
teamId: 1,
franchiseId: 2,
teamName: 'Gulls'
},
{
id: 2,
teamId: 1,
franchiseId: 2,
teamName: 'Gulls'
}
]
const getComparer = keys => (a, b) => {
return keys.every(k => _.get(a, k) === _.get(b, k))
}
const result = _.uniqWith(objectWithDuplicates, getComparer(['teamName', 'teamId']))
console.log(result)<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.6/lodash.min.js"></script>
https://stackoverflow.com/questions/53135069
复制相似问题