0

I have an javascript object returned from c# class library. Object is simple name - value pairs list.

I'm using

for (var myList in myListItems) {
  console.log(myList);
  console.log(myListItems[myList]);
}

to retrieve all items in list, but next to my items, i'm getting a bunch of .toString methods and several others, probably inherited from something.

Is there a way to remove all this methods, and get only data i need?

4
  • 1
    Without seeing more code/context it's hard to say exactly, but a check for hasOwnProperty would probably help. Commented Nov 24, 2015 at 17:02
  • 1
    for...in will iterate over all enumerable properties of the object and its prototype(s). If you can clearly identify the characteristics of the properties you don't want, then there might be a way to do that. But your current description is incomplete / vague. Commented Nov 24, 2015 at 17:06
  • You got a solution, but it's not clear yet why it works. This question isn't really useful until we know the structure of your data and know why hasOwnProperty works in this case. Commented Nov 24, 2015 at 19:27
  • I have c# method that do some calculations and returns a bunch of name - value pair items to javascript. In javascript i need to print out all that name - value pairs to screen. For some reason, along name - pair values, few .toString methods show up. I guess that's what c# compiler adds to the code... My question was how to remove that .toString methods, .hasOwnProperty worked, i don't know how and why (i'm c# guy with only basic javascript knowledge)... Commented Nov 24, 2015 at 19:33

2 Answers 2

1

Alexey is correct about using Object.hasOwnProperty

Here's an example using your code:

for (var myList in myListItems) {
  // Check if myList is a property on myListItems
  if (myListItems.hasOwnProperty(myList)) {
    console.log(myList);
    console.log(myListItems[myList]);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

The simpliest way is to use special JS function Object.prototype.hasOwnProperty()

var buz = {
  fog: 'stack'
};

for (var name in buz) {
  if (buz.hasOwnProperty(name)) {
    console.log('this is fog (' + name + ') for sure. Value: ' + buz[name]);
  }
  else {
    console.log(name); // toString or something else
  }
}

More info here: JS Mozilla Docs

7 Comments

Your example would be better if it was a modification of the OP's code, rather than a copy/paste of the MDN article.
What if toString is defined on the object itself?
@Felix Kling, of course, you are rigth, if toString method is defined on the object itself, toString will be also defined as "internal" property, but as author said, JS objects are returned from C# and I suppose it's just pure JSON objects without any functions.
"I suppose it's just pure JSON objects without any functions" Then why does toString show up in the loop? I think the question needs some clarifying.
@AlexeySobolev: But all the properties on Object.prototype are not enumerable, hence they are not iterated over with for...in.
|

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.