首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >函数插入.在环路里?

函数插入.在环路里?
EN

Stack Overflow用户
提问于 2018-11-03 20:07:54
回答 2查看 61关注 0票数 0

问题:我没有声明的函数是附加到一个变量的内存空间,我从来没有分配给它?

背景:我构建了一个脚本,通过比较对象键的值来删除重复的对象。在初始化checkSum变量和紧接着的For...in loop之后,将函数goodDiver的“鬼”值添加到变量k中。

代码语言:javascript
复制
'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?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-11-03 20:19:58

我怀疑您的鬼函数来自一个原型链(for..in循环遍历整个链)。您可以尝试使用Object.getOwnPropertyNames()Object.entries()

票数 0
EN

Stack Overflow用户

发布于 2018-11-03 23:31:35

虽然这并不是对您的问题的直接回答,但是看起来您可以更容易地使用房客实现您想要的功能。get执行与您包含的库相同的操作,uniqWith执行筛选。例如:

代码语言:javascript
复制
 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)
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.6/lodash.min.js"></script>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53135069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档