Use this tag for questions related to JavaScript objects.
2
votes
3answers
37 views
String compare gives wrong output when white space is present in string
I am having two string :
x = "hi hemant how r u"
y = "hi hemant how r u"
If we see, both look same, but
x === y gives false.
I check ascii values of both, this are different
x = "hi hemant how ...
0
votes
0answers
23 views
how to check for null or undefined in Javascript for an ID [duplicate]
I have a situation to check if a LINK made of "a href" is hidden or not. All good.
But we use same javascript in many other pages. So some pages might not have this element (I mean ID). How do I make ...
1
vote
2answers
26 views
localStorage Array of Objects: Only push new object into array if unique
I have read through a number of Stack Overflow questions but none appear to be relevant to the problem I'm trying to solve.
I have an array of objects that I'm saving in localStorage which looks like ...
1
vote
3answers
47 views
Object array's object properties changing when returning array
I have a function that has been made to manipulate and create an object array and add a position key to each item within the array, but it seems to not be working properly at all...
My function:
var ...
0
votes
3answers
33 views
Javascript variable scope for objects created within function
Consider this code,
var getPerson = function() {
var _name = "";
var person = {};
person.getName = function() {
return _name;
}
person.setName = function(value) {
...
0
votes
3answers
44 views
How to get a value in an array that's in a list of arrays in javascript
To populate a HTML select field, I have this data:
let options = [
{'label': '--Choose--', 'value':''},
{'label': 'Linux', 'value':'linux'},
{'label': 'OSX', 'value':'mac'},
{'label': 'Windows ...
1
vote
1answer
30 views
Losing context in JS function call chain
I have got some JS "objects" I'm using for some complexe stuff. So the following code is reduced to the needed minimal informations:
function AttributeWrapper(model) {
var self = this;
var ...
0
votes
2answers
52 views
How to make this function not return a negative date diffence?
The function is trying to fund the difference between to dates, but I am struggling to not return a negative number if the date is past a certain point. I have tried a few work around like using ABS ...
-1
votes
0answers
15 views
How to set up a static methods in javascript objects? [duplicate]
I would like to add methods to some javascript objects to examine their input data to see if it appears to be valid. I would also like to be able - if possible - to call these class methods without ...
0
votes
2answers
26 views
How to display assn image randomly on a web browser using dom concept?
First by creating an image object using create element how do i set its attribute for top and left part of it in a function such that it displays the image at a random position in web??
My code ...
0
votes
1answer
36 views
how to create a todo list in javascript
I want to know how to create todo list, rather than coding I need the approach
of it, like how to analyse the problem and how to map it to coding? I just want to
how to approach for any sort of ...
-1
votes
2answers
61 views
How to convert a string to JSON format?
I was getting list in array form. So at first place i converted array list to string-
var myJsonString = JSON.stringify(result);
myJsonString="[{"productId":"PI_NAME",
...
0
votes
1answer
23 views
How can I assign different setinterval functions for every link on my page? [on hold]
How to set multiple setinterval function for each link on my page dynamically
-1
votes
0answers
54 views
How to get multiple JS functions to communicate with each other?
Here is what I was having an issue with - I’m creating a trivia game in JS and have a function that takes in two parameters, both arrays. The first array is called players that has properties for ...
582
votes
26answers
443k views
Find object by id in an array of JavaScript objects
I've got an array:
myArray = [{'id':'73','foo':'bar'},{'id':'45','foo':'bar'}, etc.]
I'm unable to change the structure of the array. I'm being passed an id of 45, and I want to get 'bar' for that ...
1
vote
2answers
54 views
Is it possible to pollute the “this” object with elements and properties
A while a go I realized using jQuery for a project was a bit over-kill
because I only used 10 methods, at most...
So I decided to create a much smaller library with an API-style like jQuery.
The ...
0
votes
3answers
20 views
passing Object into Array
var initGrid = function() {
//creating a grid while initialising it
var grid = [];
// declaring each grid element as a Object having three properties
var gridElement = {
x: ...
2
votes
4answers
45 views
How do you declare variables x1, x2,…,x10 using a loop function in JavaScript?
I am trying to run the following code in JavaScript to accomplish the following task: declare a series of variables that essentially have the same name but are only different in their suffix, which is ...
0
votes
1answer
38 views
Creating a new array combining two existing objects through iteration with condition in jQuery
I have a main object consisting of two main properties, data which contains messages and included which contains the senders of the messages. I want to create a new Array called messages which will ...
2
votes
2answers
43 views
How do i dynamically put delimiters in an array in jquery?
am new here so i am still trying to get a hang of things.
how do i dynamically add a delimiter like "|" after every 3 elements in an array in jquery so i can then explode the array and use the ...
-1
votes
2answers
25 views
javascript Binding function contexts
I saw this code in a JavaScript ninja's book
<button id="test">Click Me!</button>
<script type="text/javascript">
var button = {
clicked: false,
click: function(){
this.clicked = ...
1
vote
1answer
30 views
If key is not defined then variable name is taken as key in javascript object
Lets consider the following example
a = [1,2]
c = {a, b: "Hello"}
Output in chrome dev tool
{
"a": [
1,
2
],
"b": "Hello"
}
How this is happening is it save to create a object like ...
2
votes
4answers
34 views
Function object __proto__ and prototype property
I am trying to figure out the prototype chain of a function that I wrote
function Animal(voice)
{
this.voice = voice|| 'meaw'
}
Animal.prototype.speak =function() {
console.log(this.voice);
...
4
votes
3answers
867 views
JavaScript “this” references wrong object [duplicate]
Well, this doesn't really refer to the wrong object, but I do not know how to refer to the correct one.
function someObj() {
this.someMethod1 = function() {
var elementBtn = ...
971
votes
22answers
664k views
Length of a JavaScript object (that is, associative array)
If I have a JavaScript associative array, say:
var myArray = new Object();
myArray["firstname"] = "Gareth";
myArray["lastname"] = "Simpson";
myArray["age"] = 21;
Is there a built-in or accepted ...
1
vote
1answer
31 views
How to access an element inside object array?
I have a JSON response as follows in the form of [object Object] under variable data :
{
"name": "John Johnson",
"street": "Oslo West 16",
"posts": {
"id1": "121331",
...
-1
votes
1answer
23 views
JS Context (?) process
I have a js variable like that:
var commands = {
hello: {
command: "hello",
action: "say hello"
},
open_paint: {
command: "open paint",
action: "open ...
0
votes
1answer
24 views
How to print a new object's property value in Angular?
I'm working on a shopping list app where initially the user is presented with an input and button. In the input the user enters the name of a new list, and after the button is clicked, an empty list ...
0
votes
2answers
28 views
Understanding the benefit of naming a returned object in the module pattern in JavaScript
I understand stylistically why you would name a returned object, for organization etc. However, I don't understand the benefit of adding or removing methods or the ability to update properties at ...
1
vote
1answer
47 views
Why do I have to store this Javascript object in a variable before pushing it to an array?
I'm generating a tree in Javascript using this recursive function, which works:
function generate(depth){
console.log('call '+depth);
created_children = [];
if (depth < 3) ...
4
votes
3answers
44 views
__Proto object in javascript
When considering this simple example
<script>
function test()
{
this.x = 10;
}
var obj = new test();
</script>
I read in on one of the blogs that when we use a new keyword , a proto ...
3
votes
2answers
36 views
Javascript (sub-) Object access by array of keys
Is it possible to set a Variable in an Object by an Array of Keys?
For example i have this Object:
var obj = {'outer': {'inner': 'value'} };
and want to set the Value selected by an Array of Keys:
...
0
votes
1answer
24 views
How to add objects to a key within another dynamically created object in Angular?
I'm using Angular to create a shopping list app. For testing purposes I have two pre-created lists each with pre-created names and items. Eventually the user will initially be presented with an empty ...
0
votes
1answer
47 views
Update values inside javascript object
I am working with the Affirm javascript API and I need to be able to update the values inside the checkout object but am having trouble doing so. I have tried what is mentioned here but it isnt ...
1
vote
3answers
48 views
Can a method of an object store the name of the object itself to be recovered as a string in JavaScript?
if I have:
x=3;
FirstNames = ["John","Paul","George","Ringo"];
LastNames = ["Smith","Jonson","Jones","Doe"];
SemiRandomNumbers= [3,6,7,4,2,1,0,9];
I can set:
ArrayToUse = FirstNames;
...
0
votes
2answers
45 views
Calling a function on click of OK button in a pop up in the Javascript
I have a function in javascript that displays a pop-up. I am doing this in a Sitecore based MVC project and hence I can not do the events in the normal way but following the practice that is followed ...
1
vote
1answer
42 views
Vue js reactivity issue when updating objects inside array used in v-for
I have an application using vue.js. I generate a list of results using v-for in an array of objects.
when I update the object inside nth item in array, using underscore _.extend, the view of project ...
-2
votes
3answers
38 views
javascript object methods prototype
What is the diference in the below aproachs. Which is the best way?
var obj;
obj = function () {
}
obj.prototype = {
getName: function () {
...
0
votes
1answer
26 views
How do I remove dynamically added objects from an array with a button associated with each object?
I'm using Angular to build a shopping list app. I have two pre-created lists, each with two and three pre-created items respectively for testing purposes. Of course eventually there will be no ...
0
votes
0answers
11 views
Copied object generated by Object.Assign() function has side effect?
I have an object named result which is composed of two objects like :
const a = {bar: {baz: 2}};
var b = {foo: 1};
var result = Object.assign({}, a, b);
console.log(result, a, b);
// result -> ...
6
votes
6answers
3k views
Mongoose/MongoDB result fields appear undefined in Javascript
Is there something that I'm missing that would allow item to log as an object with a parameter, but when I try to access that parameter, it's undefined?
What I've tried so far:
console.log(item) => ...
1
vote
3answers
36 views
js array.IndexOf not working with objects?
I am trying to create an array with objects holding day and time. I am looping over a source where there may be duplicates so I want to check each time that I don't already have the current day and ...
-1
votes
1answer
17 views
Javascript: How to get callback's parameters
I'm trying to somewhat replicate the functionality of forEach so that I can understand it better. In forEach you can pass it in an anonymous function with a parameter 'item' and then it would give you ...
0
votes
2answers
37 views
explanation on javascript function call passing this
I am reading the "Javascript the Good Parts" book and trying out examples to understand the concepts. I came across one example and could not understand. Please look at the code below and let me ...
0
votes
2answers
25 views
javascript MVC structure, map model to rest api
I am trying to make a model (the M in MVC) in my web application, to understand the concept. I am struggling with the async problem that occurs in my code below:
function FruitModel(api) {
...
0
votes
2answers
54 views
How to make the value of one variable of one javascript function available in any other function?
I want the value of someDate to be available to my datepicker's minDate. At present I have kept it as new Date() for it to work, but in place of this I need someDate value. someDate logic is there in ...
0
votes
2answers
32 views
looping through object array returns 'undefined'
I have two arrays.
var fruits = [];
var tasks = [];
When I enter a value in the text field it fires a function that pushes the value to an array. It then fires a separate function that ...
0
votes
2answers
37 views
Generating N urls to make AJAX requests
I'm facing a big problem after creating the API of a backend. I have a form with a GET AJAX method to filter and get results. It works fine but as the urls are generated from the users choice, there ...
7
votes
2answers
182 views
Javascript methods that can not be called from jquery objects?
I was reading Learning jQuery 1.3 (Jonathan Chaffer and Karl Swedberg) and while sorting table, they used .get() before calling .sort(), and said
we need to transform jQuery objects into array of ...
0
votes
1answer
22 views
complex json into HTML table using javascript
var resData = {"request" : { "empid(Number)":"employeeid(Number)" , "msisdn(Number)":"empmsisdn(Number)" , "num(String)":"number(String)" , "add(String)":"address(String)" } , " response" : ...