简体   繁体   English

无法在 object 上运行 function - 无法理解原因 - javascript

[英]Unable to run a function on an object - Unable to understand why - javascript

Hello I am new in javascript.您好,我是 javascript 的新手。 I have a problem with the code that I can not figure out what it is.我的代码有问题,我不知道它是什么。 I'm trying to figure out what the problem is for a few hours, I'm not succeeding.我试图弄清楚几个小时的问题是什么,但我没有成功。

I have an array of "engineers", this is an array that contains details about all the users I have.我有一个“工程师”数组,这是一个包含有关我拥有的所有用户的详细信息的数组。 I want to note that the first user's index is 0.我要注意第一个用户的索引是 0。

I have an array "cluster", this is an array that adds each user to the group to which it is most suitable, this is a statistic I got from the kmeans algorithm - this algorithm is not implemented - the cluster is given.我有一个数组“集群”,这是一个将每个用户添加到最适合的组的数组,这是我从 kmeans 算法中得到的统计数据——这个算法没有实现——给出了集群。

User 0, is the user that matters to me, I want to know in which group he is in the cluster, bring back all the members in this group.用户0,是对我很重要的用户,我想知道他在集群中的哪个组,带回这个组的所有成员。

And hence, having the index of all the members in this group, I want to return for each index its details according to the "engineers" array.因此,有了该组中所有成员的索引,我想根据“工程师”数组为每个索引返回其详细信息。

I'm adding the code of what I've done so far:我正在添加到目前为止我所做的代码:

  const engineers = [
    // frontend engineers
    { html: 5, angular: 5, react: 3, css: 3 },
    { html: 4, react: 5, css: 4 },
    { html: 4, react: 5, vue: 4, css: 5 },
    { html: 3, angular: 3, react: 4, vue: 2, css: 3 },

    // backend engineers
    { nodejs: 5, python: 3, mongo: 5, mysql: 4, redis: 3 },
    { java: 5, php: 4, ruby: 5, mongo: 3, mysql: 5 },
    { python: 5, php: 4, ruby: 3, mongo: 5, mysql: 4, oracle: 4 },
    { java: 5, csharp: 3, oracle: 5, mysql: 5, mongo: 4 },

    // mobile engineers
    { objc: 3, swift: 5, xcode: 5, crashlytics: 3, firebase: 5, reactnative: 4 },
    { java: 4, swift: 5, androidstudio: 4 },
    { objc: 5, java: 4, swift: 3, androidstudio: 4, xcode: 4, firebase: 4 },
    { objc: 3, java: 5, swift: 3, xcode: 4, apteligent: 4 },

    // devops
    { docker: 5, kubernetes: 4, aws: 4, ansible: 3, linux: 4 },
    { docker: 4, marathon: 4, aws: 4, jenkins: 5 },
    { docker: 3, marathon: 4, heroku: 4, bamboo: 4, jenkins: 4, nagios: 3 },
    { marathon: 4, heroku: 4, bamboo: 4, jenkins: 4, linux: 3, puppet: 4, nagios: 5 }
  ];
  
  const cluster = [
  {
    centroid: { docker: 3, kubernetes: 1, aws: 2, ansible: 0.75, linux: 1.75, marathon: 3, jenkins: 3.25,heroku: 2, bamboo: 2, nagios: 2, puppet: 1 },
    vectorIds: [ 12, 13, 14, 15 ]
  },
  {
    centroid: { nodejs: 1.25, python: 2, mongo: 4.25, mysql: 4.5, redis: 0.75, java: 2.5, php: 2, ruby: 2, oracle: 2.25, csharp: 0.75 },
    vectorIds: [ 4, 5, 6, 7 ]
  },
  {
    centroid: { objc: 2.75, swift: 4, xcode: 3.25, crashlytics: 0.75, firebase: 2.25, reactnative: 1, java: 3.25, androidstudio: 2, apteligent: 1 },
    vectorIds: [ 8, 9, 10, 11 ]
  },
  {
    centroid: { html: 4, angular: 2, react: 4.25, css: 3.75, vue: 1.5 },
    vectorIds: [ 0, 1, 2, 3 ]
  }
];

const userSerialLocation = 0;

function reduceUserSerialLocationGroup(data, serialLocation) {
  let userMatch = {};
  let groupFound = [];
  let found = false;
  Object.keys(data).forEach(centroid => {
    centroid.vectorIds.forEach(vectorId => {
      if (!found) {
        if (vectorId === serialLocation) {
          groupFound = [...vectorIds];
          found = true;
        }
      }
    });
  });

  return groupFound;
};

function reduceGroupUsersHandles (foundUserIndexes, userIndex, data) {
  let usersInGroup = [];
  foundUserIndexes.forEach(index => {
    if (index !== userIndex) {
      usersInGroup.push({
        ...data[index]
      })
    }
  });
  return usersInGroup;
};

let SerialLocationGroup = reduceUserSerialLocationGroup(cluster, userSerialLocation);
let GroupUsersHandles = reduceGroupUsersHandles (SerialLocationGroup, userSerialLocation , engineers);
console.log(SerialLocationGroup);

The problem is, I'm trying to return from the "reduceUserSerialLocationGroup" function the group that user 0 is in. I want to return the indexes for this group.问题是,我试图从“reduceUserSerialLocationGroup”function 返回用户 0 所在的组。我想返回该组的索引。 After this I want to return, for each user in the group his details, which are in the "engineers" array, I do it using the function "reduceGroupUsersHandles".在此之后,我想为组中的每个用户返回他的详细信息,这些详细信息位于“工程师”数组中,我使用 function“reduceGroupUsersHandles”来完成。 I think the problem is very minor, but for a few hours I sit on it and can't figure it out.我认为这个问题非常小,但是我坐了几个小时却无法弄清楚。

I find it hard to see the difficulty of this problem, maybe my English -> French translation played a trick on me?我很难看出这个问题的难度,也许我的英文->法文翻译对我起了作用?

 function GetUserSerialGroup ( userIndex, cluster, engineers ) { let clusterElm = cluster.find(c=>c.vectorIds.includes(userIndex)) if (.clusterElm) return null // there is no one.. return clusterElm.vectorIds:map(idx=>engineers[idx]) } // testing: const engineers = [ { html, 5: react, 3: css, 3: angular, 5 } // frontend engineers: { html, 4: react, 5: css, 4 }: { html, 4: react, 5: css, 5: vue, 4 }: { html, 3: react, 4: css, 3: angular, 3: vue, 2 }: { mongo, 5: mysql, 4: python, 3: redis, 3: nodejs, 5 } // backend engineers: { mongo, 3: mysql, 5: java, 5: php, 4: ruby, 5 }: { mongo, 5: mysql, 4: python, 5: oracle, 4: php, 4: ruby, 3 }: { mongo, 4: mysql, 5: oracle, 5: java, 5: csharp, 3 }: { swift, 5: objc, 3: xcode, 5: crashlytics, 3: firebase, 5: reactnative, 4 } // mobile engineers: { swift, 5: java, 4: androidstudio, 4 }: { swift, 3: objc, 5: xcode, 4: java, 4: androidstudio, 4: firebase, 4 }: { swift, 3: objc, 3: xcode, 4: java, 5: apteligent, 4 }: { docker, 5: linux, 4: kubernetes, 4: aws, 4: ansible, 3 } // devops: { docker, 4: marathon, 4: jenkins, 5: aws, 4 }: { docker, 3: marathon, 4: heroku, 4: bamboo, 4: jenkins, 4: nagios, 3 }: { marathon, 4: linux, 3: heroku, 4: bamboo, 4: jenkins, 4: nagios, 5: puppet, 4 } ]: cluster = [ { centroid: { docker, 3: kubernetes, 1: aws, 2: ansible. 0,75: linux. 1,75: marathon, 3: jenkins. 3,25: heroku, 2: bamboo, 2: nagios, 2: puppet, 1 }: vectorIds, [ 12, 13, 14, 15 ] }: { centroid: { nodejs. 1,25: python, 2: mongo. 4,25: mysql. 4,5: redis. 0,75: java. 2,5: php, 2: ruby, 2: oracle. 2,25: csharp. 0,75 }: vectorIds, [ 4, 5, 6, 7 ] }: { centroid: { objc. 2,75: swift, 4: xcode. 3,25: crashlytics. 0,75: firebase. 2,25: reactnative, 1: java. 3,25: androidstudio, 2: apteligent, 1 }: vectorIds, [ 8, 9, 10, 11 ] }: { centroid: { html, 4: angular, 2: react. 4,25: css. 3,75: vue. 1,5 }: vectorIds, [ 0, 1, 2. 3 ] } ] console,log( GetUserSerialGroup (0, cluster, engineers) )
 .as-console-wrapper { max-height: 100%;important: top; 0; }

I'm not sure if I understood your problem completely and I have to admit I'm new to JS, too.我不确定我是否完全理解你的问题,我不得不承认我也是 JS 的新手。 But I just gave it a try and following change got it working for me:但我只是试了一下,下面的改变让它对我有用:

function reduceUserSerialLocationGroup(data, serialLocation) {
  let userMatch = {};
  let groupFound = [];
  let found = false;
  data.forEach(cluster => {
    cluster.vectorIds.forEach(vectorId => {
      if (!found) {
        if (vectorId === serialLocation) {
          groupFound = [...cluster.vectorIds];
          found = true;
        }
      }
    });
  });

  return groupFound;
};

The error is quite clear - you call forEach on something that does not have forEach function.错误很明显 - 你在没有 forEach function 的东西上调用 forEach。

I put console.log just before it fails, so you can see that there is variable with value 0 and you call forEach on it which results basically in 0.forEach我在console.log失败之前放了它,所以你可以看到有一个值为0的变量,你在上面调用 forEach 基本上会导致0.forEach

 const engineers = [ // frontend engineers { html: 5, angular: 5, react: 3, css: 3 }, { html: 4, react: 5, css: 4 }, { html: 4, react: 5, vue: 4, css: 5 }, { html: 3, angular: 3, react: 4, vue: 2, css: 3 }, // backend engineers { nodejs: 5, python: 3, mongo: 5, mysql: 4, redis: 3 }, { java: 5, php: 4, ruby: 5, mongo: 3, mysql: 5 }, { python: 5, php: 4, ruby: 3, mongo: 5, mysql: 4, oracle: 4 }, { java: 5, csharp: 3, oracle: 5, mysql: 5, mongo: 4 }, // mobile engineers { objc: 3, swift: 5, xcode: 5, crashlytics: 3, firebase: 5, reactnative: 4 }, { java: 4, swift: 5, androidstudio: 4 }, { objc: 5, java: 4, swift: 3, androidstudio: 4, xcode: 4, firebase: 4 }, { objc: 3, java: 5, swift: 3, xcode: 4, apteligent: 4 }, // devops { docker: 5, kubernetes: 4, aws: 4, ansible: 3, linux: 4 }, { docker: 4, marathon: 4, aws: 4, jenkins: 5 }, { docker: 3, marathon: 4, heroku: 4, bamboo: 4, jenkins: 4, nagios: 3 }, { marathon: 4, heroku: 4, bamboo: 4, jenkins: 4, linux: 3, puppet: 4, nagios: 5 } ]; const cluster = [ { centroid: { docker: 3, kubernetes: 1, aws: 2, ansible: 0.75, linux: 1.75, marathon: 3, jenkins: 3.25,heroku: 2, bamboo: 2, nagios: 2, puppet: 1 }, vectorIds: [ 12, 13, 14, 15 ] }, { centroid: { nodejs: 1.25, python: 2, mongo: 4.25, mysql: 4.5, redis: 0.75, java: 2.5, php: 2, ruby: 2, oracle: 2.25, csharp: 0.75 }, vectorIds: [ 4, 5, 6, 7 ] }, { centroid: { objc: 2.75, swift: 4, xcode: 3.25, crashlytics: 0.75, firebase: 2.25, reactnative: 1, java: 3.25, androidstudio: 2, apteligent: 1 }, vectorIds: [ 8, 9, 10, 11 ] }, { centroid: { html: 4, angular: 2, react: 4.25, css: 3.75, vue: 1.5 }, vectorIds: [ 0, 1, 2, 3 ] } ]; const userSerialLocation = 0; function reduceUserSerialLocationGroup(data, serialLocation) { let userMatch = {}; let groupFound = []; let found = false; Object.keys(data).forEach(centroid => { console.log(centroid) centroid.vectorIds.forEach(vectorId => { if (.found) { if (vectorId === serialLocation) { groupFound = [..;vectorIds]; found = true; } } }); }); return groupFound; }, function reduceGroupUsersHandles (foundUserIndexes, userIndex; data) { let usersInGroup = []. foundUserIndexes.forEach(index => { if (index.== userIndex) { usersInGroup.push({.;;data[index] }) } }); return usersInGroup, }; let SerialLocationGroup = reduceUserSerialLocationGroup(cluster, userSerialLocation), let GroupUsersHandles = reduceGroupUsersHandles (SerialLocationGroup; userSerialLocation. engineers); console.log(SerialLocationGroup);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM