LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-13-2015, 02:59 AM   #1
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
[Javascript] How to push/pop values from: var x = [{x: 30, y: 40}]?


Written in Javascript:
Code:
var data =  [{value: 30, color: "blue"}  ]
I want to push and remove some values from it.
I tried:
Code:
data.push (10, "red") 
data.push (111, "green") 
data.push (1112, "blue")  
console.log (data) console.log (data[0].value) 
console.log (data[1].value)
which resulted in:
Code:
qml: [[object Object],10,red,111,green,1112,blue]
qml: 30
qml: undefined
1. The first value present by default in the structure is not shown in console with first print.

2. I am not able to see the value of the first pushed item with third print.


Where am I going wrong?

Last edited by Aquarius_Girl; 07-13-2015 at 03:49 AM.
 
Old 07-13-2015, 03:14 AM   #2
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Correct method of pushing is this:
Code:
data.push (
            {
              value: 20,
              color: "red"
             }
           )
 
Old 07-13-2015, 08:24 AM   #3
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
You've gotten the correct answer already, but just to clear some confusion you might have, push accepts variable number of arguments and each is appended to the array. In other words, the following two snippets do the same thing:
Code:
var arr = [];
arr.push('foo', 'bar');
Code:
var arr = [];
arr.push('foo');
arr.push('bar');

Last edited by mina86; 07-13-2015 at 09:30 AM.
 
Old 07-14-2015, 12:17 AM   #4
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
In javascript:
  • Is there any special meaning for words value: and color: ?
  • Does a : impart special meaning?
 
Old 07-14-2015, 07:12 AM   #5
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
No, value and color are normal identifiers. (So is colour in case you were wondering).

{foo: bar, } is an object literal it creates an object with property foo equal to value of bar. var x = {value: 20, color: "red"} is equivalent to:
Code:
var x = {};  // empty Object with no own properties
x.value = 20;
x.color = "red";
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript pop-up outlook client byran cheung Programming 2 01-17-2014 12:39 PM
my.cnf on debian squeeze. var names don't match, var values missing. sneakyimp Linux - Server 1 07-03-2011 01:59 AM
Push email for POP or IMAP? resetreset Linux - Newbie 21 01-14-2011 01:57 AM
why get compiler error: suffix or operands invalid for 'push' (or 'pop') maxreason Programming 1 08-10-2008 10:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration