Iterate over Javascript object [duplicate] - javascript

This question already has answers here:
How do I iterate over a JSON structure? [duplicate]
(13 answers)
Closed 8 years ago.
I have a Javascript object
var a = {
"tag1": "Stocks",
"acctType1": "individual",
"compare1": "contains",
"match_name1": "scrapedaccounttype",
"text1": "dog ",
"tag2": "Stocks",
"acctType2": "individual",
"compare2": "contains",
"match_name2": "scrapedaccounttype",
"text2": "cat"
}
I need to use this Javascript object to do some more math, but I am not sure about how I would be iterating over the Javascript object.
I can have any number of tags (tag1, tag2, tag3, tag4 ... ) or similarly other keys like (acctType1, acctType2, acctType3.... ) so I need to iterate over them individually and do some manipulation to use these variables in a different function.
While , for each what would help my cause here. Note that I could have any number of tags(tag1,tag2...) or comapare(compare1, compare2, compare3..)
I would need to process all of them individually.

What you have is NOT JSON. It is JavaScript defining an Object literal, commonly referred to as a JS object. JSON (JavaScript Object Notation) is a string that can be parsed to produce an object literal.
For your JS object (which has ample online documentation), you can iterate over the object keys by:
var a={"tag1":"Stocks","acctType1":"individual","compare1":"contains","match_name1":"scrapedaccounttype","text1":"dog ","tag2":"Stocks","acctType2":"individual","compare2":"contains","match_name2":"scrapedaccounttype","text2":"cat"}
Object.keys(a).forEach(function (k) {
console.log(a[k]);
});
Or:
for (var key in a) {
if (a.hasOwnProperty(key)) {
console.log(a[k]);
}
}

Related

How to access a particular path/adress inside an object? [duplicate]

This question already has answers here:
access object through dot-syntax string path
(2 answers)
Convert a JavaScript string in dot notation into an object reference
(34 answers)
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 2 years ago.
I am having a problem which I think I might have figured out before how to do it but I can't remember now and can't figure it out.
Let's say we have an object thats a few levels deep, meaning it has as values other objects which also have as some of the values objects and so on.
Now how could I make a function to which I pass the object and and adress inside it and I can access the value at that location inside the function like this:
const getValueAtAdress = (object, 'country.city.rules') => {
return //here I need to return the value at object.country.city.rules.
}
Am I missing something obvious?
I thought I'd mention here for posterity that what helped me was the answer using the reduce which is exactly what I used before but I could not remember:
Example that I am using for my particular problem:
let stateLocation = address.split('.').reduce((acc, cur) => acc[cur], state);
Your code shows a function declaration but you can't declare an argument name in quotes
You can however call a function and pass a string.
In that case, you just need to split the string into an array and then loop over that array, building up a "chained" set of string indexes that can be passed to the object. The String.split() and Array.reduce() methods are the key.
let obj = {
county: {
city: {
rules: "Strict"
}
}
};
const getValueAtAddress = (object, countyCityRules) => {
// Split the string at the dots to form an array...
// The loop over that array and reduce it with an
// accumulator that is then applied to the object.
return countyCityRules.split(".").reduce((acc, cur) => acc[cur], obj);;
}
console.log(getValueAtAddress(obj, "county"));
console.log(getValueAtAddress(obj, "county.city"));
console.log(getValueAtAddress(obj, "county.city.rules"));

Why does JavaScript allow accessing an object's property using an array of length 1, as if the array was just the single element itself? [duplicate]

This question already has answers here:
Why does JavaScript convert an array of one string to a string, when used as an object key? [duplicate]
(4 answers)
Why can I access object property with an array?
(2 answers)
Closed 2 years ago.
See this example:
> map = { 5: 'five' }
> map[5]
'five' // Makes sense
> arr = [5]
> map[arr]
'five' // Does not make sense
> arr = [5,6,7]
> map[arr]
undefined // I'm cool with that
So when I access an object's property with an array of length 1, it behaves as if I simply input the single element instead of the array. But when the array's length is anything but 1, it will produce the expected behavior.
What is the purpose of this behavior? To me this would simply make things harder for a developer to find a bug in their code.
If an object index is not a string or Symbol, it's converted to a string as if by its toString() method.
array.toString() performs the equivalent array.join(",").
So
map[arr]
is equivalent to
map[arr.join(",")]
If the array has one element, joining it simply returns that one element converted to a string, so it's equivalent to
map[arr[0].toString()]
and that explains your first result.
But in your second example, it returns the string "5,6,7" and there's no property with that name in the object.

getJSON replace object with variable [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 4 years ago.
I have json file called list.json:
{
"nvg":{
"Title":"title",
"Description":"description",
"Image":"",
"URL":{
"DEV":"https://dev.com",
"TEST":"https://test.com",
"PROD":"https://prod.com"
}
}
I have an array
var apps = ["nvg"];
I want to access the json data and extract the object based on the variable's value. Does anyone know how to pass the [i] into the json call?
var getConfig = $.getJSON( data, function( json ) {
var apps = ["nvg"];
apps.forEach(function(i){
alert(i);
alert(json.i.URL.DEV);
});
});
Like the code above, but to actually the "i" to pass the value defined in the array?
You should be able to access the data using your Javascript object like an object or array. This means you can do the following:
alert(json[i].URL.DEV);

Array of an already defined object in Javascript [duplicate]

This question already has an answer here:
creating arrays of objects in javascript
(1 answer)
Closed 5 years ago.
I have an object defined as
myObject = {
property1 : "",
property2 : ""
}
I need to create an Array of the above object.
I have tried using
myArray = myObject[].
But it doesn't seem to work.Is there any way that we can create an array of a predefined object in javascript.
Is there any way that we can create an array of a predefined object in javascript?
No, in JavaScript, you can't type the content of your array at the declaration of your array.
What you are looking for is TypeScript.

Object and Object Literals in javascript [duplicate]

This question already has answers here:
What is the difference between `new Object()` and object literal notation?
(12 answers)
Closed 6 years ago.
Will anyone please explain the difference between object and object literals in JavaScript?
So far I learned by searching google is given bellow:
1) Object is a collection of name-value pairs like: address:"my address".
2) Object Literals are a sequence of name-value pairs separated by commas and surrounded by curly braces. For example: {address: "my address", roll: 0001}
But its still not making sense to me. I can't find out the basic differences between these two. Actually, I'm confused with the 'collection of name-value pair' and 'sequence of name-value pairs'.
An object literal is simply an object that is literally defined, as in
var object_literal = {
key1 : "value",
key2 : "value2",
}
However there are many types of objects in javascript, for instance
var obj1 = new Date(); // object
var obj2 = function() {}; // object
var obj3 = new RegExp(); // object
and many, many more, but these are not literal objects

Categories