Skip to main content
  1. About
  2. For Teams
Asked
Viewed 86 times
0

I am trying to assemble a certain string out of a JavaScript object and am having some problems.

I created a function that takes the object and should return the string. The initial object looks like so:

var testObject = {
  "Topics": ["Other", "New1"],
  "Other": ["try this", "this also"]
};

And I would like the string to spit out this:

"Topics~~Other|Topics~~New1|Other~~try this|Other~~this also"

Here is what I have now:

var testObject = {
  "Topics": ["Other", "New1"],
  "Other": ["try this", "this also"]
};

function transformObjectToString(activeFilters) {
  var newString = "";

  var checkFilterGroups = function(filterTopic) {
    activeFilters[filterTopic].map(function(selectedFilter) {
      var tempString = filterTopic + "~~" + selectedFilter + "|";
      console.log("check string", tempString);
      newString.concat(tempString);
    });
  }

  for (var filterGroup in activeFilters) {
    checkFilterGroups(filterGroup);
  }

  return newString;

}

console.log(transformObjectToString(testObject));

The temp string seems to be formatted correctly when I check the log, but, for whatever reason, it looks like the concat is not working as I assumed it would.

2
  • 1
    .concat() returns a new string so newString.concat(tempString); is not accomplishing anything because you don't assign the result back to newString. Remember, strings in Javascript are immutable so any modification always creates a new string.
    jfriend00
    –  jfriend00
    2015-12-31 05:11:24 +00:00
    Commented Dec 31, 2015 at 5:11
  • 1
    Appreciate all the answers! I would also appreciate feedback from the downvotes so I can improve my questions for the future.
    ajmajmajma
    –  ajmajmajma
    2015-12-31 05:21:54 +00:00
    Commented Dec 31, 2015 at 5:21

3 Answers 3

4

You should be able to just use += as this is just string concatenation. Then, all you must do is strip the last character. Here's a JSFiddle with the change https://jsfiddle.net/tfs98fxv/37/.

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

Comments

1

You can use .join('|')

var testObject = {
  "Topics": ["Other", "New1"],
  "Other": ["try this", "this also"]
};

function transformObjectToString(activeFilters) {
  var strings = [];

  var checkFilterGroups = function(filterTopic) {
    activeFilters[filterTopic].map(function(selectedFilter) {
      var tempString = filterTopic + "~~" + selectedFilter;
      strings.push(tempString);
    });
  }

  for (var filterGroup in activeFilters) {
    checkFilterGroups(filterGroup);
  }

  return strings.join('|');

}

console.log(transformObjectToString(testObject));

Comments

1
newString = newString.concat(tempString);

this works too.

edit: how this works is, at first newString is set to null so null + tempstring at first loop, and then the newSting is set to a value, value + tempString on second loop and so on. finally you have the concatinated string in one variable which you will be returning.

edit:edit: Also what @jfriend00 said in the comments, ditto

.concat() returns a new string so newString.concat(tempString); is not accomplishing anything because you don't assign the result back to newString. Remember, strings in Javascript are immutable so any modification always creates a new string

3 Comments

Some explanation would make this a better answer.
I meant explanation of why what the OP had didn't work.
Look at my comment below the OP's question for what I was talking about. Good answers explain what the OP was doing wrong (which you did not do) and then show how to fix it.

Your Answer

Post as a guest

Required, but never shown

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.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.