skip to Main Content

Hello I am new in 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.

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.

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.

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.
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". I think the problem is very minor, but for a few hours I sit on it and can’t figure it out.

3

Answers


  1. The error is quite clear – you call forEach on something that does not have forEach function.

    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

    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);
    Login or Signup to reply.
  2. I’m not sure if I understood your problem completely and I have to admit I’m new to JS, too. 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;
    };
    
    Login or Signup to reply.
  3. 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; }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search