Why am I getting objects printed twice?
Every expression in Ruby returns a value; in irb
, the value returned by the expression you've just executed is displayed after =>
.
The return value of Enumerable::each
is the object that called each
- in this case, the array [1,2,3,4]
My ArrayList prints the same object twice
Here:
employeeList.add(employee);
You keep adding the very same Employee object all the time.
Instead: create a completely new Employee object, and keep adding these different objects to your list!
What you do:
- take a piece of paper, to write stuff on that
- then you put that piece of paper into a binder
- then you fetch that paper again, and you write new stuff onto it
- you stuff the page into the binder
- then you fetch that paper again, and you write new stuff onto it...
Nested Object.keys() are printing properties multiple times instead of only once
your logic here of nesting the loops is wrong.
these 2 object does not seem to be connected to one another, meaning you do not need the data from the first loop in order to perform the other loops. just split it into 2 seperate loops, would save you both time and repititions:
let nameKeys = Object.keys(newData.name).map(key => newData.name[key].name);
let imagesKeys = Object.keys(newData._temp.images).map(keyImage =>
newData._temp.images[keyImage].rawFile.preview);
now you can access nameKeys
and imageKeys
whenever you want, and they will contain the values you previously logged. My naming might be a bit off tho, feel free to change that :D
Also, as others mentioned- no need for the async
keyword... you do not perform any async operation inside (yet, at least. if thats what you're planning then go ahead and keep it).
The same thing is printed twice - JS Arrays
This is because you're appending, try this:
const err1 = {
"type": new Array(),
"desc": new Array()
}
err1.type.push("a","b");
err1.desc.push("ananas","banana");
var errD = new String();
var errT = new String();
err1.type.forEach(function(el, ind){
errT = `Type: ${el}`;
errD = `Description: ${err1.desc[ind]}`;
console.log(`ERROR: \n${errT}\n${errD}`);
});
C++ Output being printed twice
"R"+i
does not append a number to a string. "R"
is an array of char
, containing the values {'R', '\0'}
. This array decays to a pointer to the first char
when you add a number to it, doing pointer arithmetic. You end up with a pointer to who-knows-what, which then gets turned into a std::string
, giving you garbage. This is called Undefined Behaviour. Your program could do absolutely anything at all, including appearing to work normally. It is very dangerous.
String literals are all stored in the same area of memory when the program runs, so "R"
is stored somewhere close to "IN MAIN"
and "null"
. I guess what is happening is that the bad pointer arithmetic moves you from the start of "R"
to the start of "IN MAIN"
, which is why that is printed. But this is definitely not guaranteed.
There are various ways to build up a string correctly, the best probably being std::to_string
mentioned by Joachim Pileborg above. Then you can use +
to concatenate std::string
objects. Another option is using an std::ostringstream
.
How do i stop my array info from printing twice?
They aren't Arrays. event1Array
and event2Array
are actually Objects
, so you don't want to loop through them. You should only be looping through the eventArray
.
for (var i = 0, len = eventArray.length; i < len; i++) {
alert("attend a " + eventArray[i].name + " starting " + eventArray[i].time[0]);
}
Here is a working jsfiddle...
Java Constructor problem, Output printed twice
When you create first object class1 obj1 = new class1();
this calls the default constructor, but see what is inside of your default constructor ? Look at the code you wrote
class1() {
this(12,8);
this.mul();
}
here what happens is , this(12,8)
calls the parameterized constructor of this class which is called chain of constructors.
class1(int x, int y){
this.x = x;
this.y = y;
this.mul();
}
and sets corresponding values to x and y and calls the mul function which comes out to be 96. And then next line evaluates and yes again it calls the same method [mul] . This is the reason you are getting 96 twice.
Why does the pointer get printed twice in C?
The first program has undefined behavior.
The expression &m does not point to a character of a string. It points to a single object of the type char
char string[] = "My string";
char m = string[0];
After the object m there can be anything in the memory. It just occurred such a way that after the object m there is the array string without any gap.
But the conversion specifier %s expects a pointer to a string that is to a sequence of characters that is terminated by the zero character '\0'.
In the second program the pointer m points to the first character of the string stored in the array string.
char string[] = "My string";
char *m = &string[0];
So the second program is correct.
Related Topics
You Have Already Activated X, But Your Gemfile Requires Y
Rails 3: Alias_Method_Chain Still Used
How to Get the Match Data For All Occurrences of a Ruby Regular Expression in a String
Understanding the "||" or Operator in If Conditionals in Ruby
Ruby 2.4 and Rails 4 Stack Level Too Deep (Systemstackerror)
Using Net::Http.Get For an Https Url
Find Indices of Elements That Match a Given Condition
Rescue_From Actioncontroller::Routingerror in Rails 4
Why Am I Getting Objects Printed Twice
Error Installing Libv8: Error: Failed to Build Gem Native Extension
Difference Between Map and Collect in Ruby
Difference Between Datetime and Time in Ruby