0

I am working on a function where I pass an object (record) to a function. It then loops over the keys and checks to see if that key is in our second object (lookup). If there is a match, it replaces the value in our record with a manipulated version (turns it into a link). If there is no match, it keeps its original value.

This is what I am passing to the function:

{ Source: "1234", NTID: "joeBob", Department: "x123", Email: '[email protected]' }

-Here is the function

function createLink2(record) {

  // Link types
  var output = {
      'ntid': 'https://example.com/profile/',
      'email': 'mailTo:'
  };

  // Vars
  var i,
      key,
      keys = Object.keys(output);

  // Loop over our object keys
  Object.keys(record).forEach(function(k, ind) {

    // Loop over each of the link types
    for ( i = 0; i < keys.length; ++i ) {
      key = keys[i];

      // If our key matches that of our object, turn it into a link
      if(k.toLowerCase() == key){
        record = '<a href="'+ output[key] + record[k] + '" target="_blank">'+record[k]+'</a>';
      }else{
        // Return the original value of the property since its not a match. Not quite sure how to do this part.
      }

    }

  });
  return record;
}

My goal here is that it would replace the value of Email with <a href="mailto: [email protected]">[email protected]</a> and NTID with <a href="https://example.com/profile/joeBob">joeBob</a>.

The issue I am having is with the return - Not quite sure how to edit the data and return the full object back.

1
  • returning a value from .forEach() doesn't do anything. You should look into .reduce() for what you are trying to do. Commented Mar 2, 2017 at 16:29

3 Answers 3

1

change this line:

record = '<a href="'+ output[key] + record[k] + '" target="_blank">'+record[k]+'</a>';

to this:

record[k] = '<a href="'+ output[key] + record[k] + '" target="_blank">'+record[k]+'</a>';

Of course, you could do this more easily by referring to the properties of the object directly:

function createLink2(record) {

  // Link types
  var output = {
      'NTID': 'https://example.com/profile/',
      'Email': 'mailTo:'
  };

  // Loop over the output keys
  Object.keys(output).forEach(function(k, ind) {
    if(record.hasOwnProperty(k)) {
      record[k] = '<a href="' + output[k] + record[k] + '">' + record[k] + '</a>';
    }
  });
  return record;
}

Note that you don't really need to return it since the contents of the object will be changed directly as mentioned by others in the comments.

Sign up to request clarification or add additional context in comments.

3 Comments

The issue though is that my properties are dynamic so the names could be something random, containing the keyword (NTID) for example which is why I didnt want to hard code them keys like that.
Understood. You could use variables for the properties in the same way. I'll edit to show it...
Of course, if you are running into case sensitivity issues on your properties then your approach with iterating through the keys and comparing in lowercase will work.
0

Javascript objects are passed by reference. So if you modify the object in the function it will be enough. Example:

function test(obj) {
  obj.a = 10
}

var x = {a: 2};
test(x);
console.log(x.a) //prints 10

So, all you have to do is modify the value of "Email" with whatever you want.

Comments

0

You can iterate over an array and return an object using .reduce()

function createLink2(record) {

  // Link types
  var output = {
    'ntid': 'https://example.com/profile/',
    'email': 'mailTo:'
  };

  // Vars
  var keys = Object.keys(output);

  // Loop over our object keys
  record = keys.reduce(function(obj, currKey) {
    if (obj[currKey] != undefined) {
      obj[currKey] = '<a href="' + output[currKey] + obj[currKey] + '" target="_blank">' + obj[currKey] + '</a>'
    }
    return obj;
  }, record);
  return record;
}

console.log(createLink2({ntid: "12345", email: "[email protected]"}));

3 Comments

In your example, what happens to the values where the key isn't in the output? Such as FirstName = bob, what happens to that?
Hm, I tried this and your demo is working fine on SO, however it is returning my values as [object Object] so I will try and figure that out
@SBB For any keys that record has, but output does not, it will simply leave them alone. It will only update the values of the properties that are on both objects. Can you try updating your question with your new issues so I can take a look at the code and try to figure out why it's happening?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.