Signup/Sign In

Answers

All questions must be answered. Here are the Answers given by this user in the Forum.

You can use the for-in loop as shown by others. However, you also have to make sure that the key you get is an actual property of an object, and doesn't come from the prototype.

Here is the snippet:

var p = {
"p1": "value1",
"p2": "value2",
"p3": "value3"
};

for (var key in p) {
if (p.hasOwnProperty(key)) {
console.log(key + " -> " + p[key]);
}
}
3 years ago
Iterate through the entrySet() like so:

public static void printMap(Map mp) {
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
it.remove(); // avoids a ConcurrentModificationException
}
}
3 years ago
Use enumerate to get the index with the element as you iterate:

***for index, item in enumerate(items):
print(index, item)***
And note that Python's indexes start at zero, so you would get 0 to 4 with the above. If you want the count, 1 to 5, do this:

***for count, item in enumerate(items, start=1):
print(count, item)***
3 years ago
In such cases you can use an iterator explicitly.
Sometimes, you might want to use an iterator explicitly. You can do that, too, although it's a lot clunkier than for-of. It looks like this:

const a = ["a", "b", "c"];
const it = a.values();
let entry;
while (!(entry = it.next()).done) {
console.log(entry.value);
}
An iterator is an object matching the Iterator definition in the specification. Its next method returns a new result object each time you call it. The result object has a property, done, telling us whether it's done, and a property value with the value for that iteration. (done is optional if it would be false, value is optional if it would be undefined.)

The meaning of value varies depending on the iterator; arrays support (at least) three functions that return iterators:

values(): This is the one I used above. It returns an iterator where each value is the array entry for that iteration ("a", "b", and "c" in the example earlier).
keys(): Returns an iterator where each value is the key for that iteration (so for our a above, that would be "0", then "1", then "2").
entries(): Returns an iterator where each value is an array in the form [key, value] for that iteration
3 years ago
As far as my knowledge the first one, ideally with a real link to follow in case the user has JavaScript disabled. Just make sure to return false to prevent the click event from firing if the JavaScript executes.

Link

If you use Angular2, this way works:

Click me.
3 years ago
There is no parent selector; just the way there is no previous sibling selector. One good reason for not having these selectors is because the browser has to traverse through all children of an element to determine whether or not a class should be applied. For example, if you wrote:

body:contains-selector(a.active) { background: red; }

Then the browser will have to wait until it has loaded and parsed everything until the to determine if the page should be red or not.
3 years ago
We can make it using fit-content property in width and height:
In this following method we set the width and height property to fit-content value.





Studytonight Example






Studytonight




css language



3 years ago
We also had to_string() built-in method in c++ that accepts a value of any basic data type and converts it into a string.

Below is an example:

#include
#include
using namespace std;

int main() {
int num = 100; // a variable of int data type

string str; // a variable of str data type
str = to_string(num); // using to_string to convert an int into a string
cout << "The integer value is " << num << endl;
cout << "The string representation of the integer is " << str << endl;
}
3 years ago
You need virtual functions if you have an object whose type is known at runtime but not known at compile time.

You may destroy an object without knowing its exact type, by calling the virtual destructor on its pointer. Here is where we use virtual destructors in C++
3 years ago
We can also use string::data function here

#include
#include
int main()
{
std::string str = "std::string to const char*";
char const *c = str.data();
std::cout << c;
return 0;
}
the above program converts std::string to a pointer of characters to string data
3 years ago