I have two different JSON responses from ebay(); and etsy();
Etsy JSON array: [{"listing_id":123,"title":"etsy","..."}]
Ebay JSON array: [{"itemId":["123"],"title":["ebay"],..,}]
Full Ebay and Etsy JSON result shown here
Question:
1. Why are there brackets over the values of the key itemID?
2. Is it possible to combine the two arrays and display it together? Will there be additional steps to retrieve the values with/without brackets?
Etsy JSON array: [{"listing_id":123,"title":"etsy","..."}]
Here, listing_id = Integer and title = String
Ebay JSON array: [{"itemId":["123"],"title":["ebay"],..,}]
Here, itemId = array of string and title = array of string
So answer your question,
itemId is an array of string that's why it is there are brackets over the value.
Yes, it is possible to combine two arrays. You need to create the new structure to store the common values.
Accessing nested data structures
A nested data structure is an array or object which refers to other arrays or objects, i.e. its values are arrays or objects. Such structures can be accessed by consecutively applying dot or bracket notation.
Here is an example:
const data = {
code: 42,
items: [{
id: 1,
name: 'foo'
}, {
id: 2,
name: 'bar'
}]
};
Let's assume we want to access the name of the second item.
Here is how we can do it step-by-step:
As we can see data is an object, hence we can access its properties using dot notation. The items property is accessed as follows:
data.items
The value is an array, to access its second element, we have to use bracket notation:
data.items[1]
This value is an object and we use dot notation again to access the name property. So we eventually get:
const item_name = data.items[1].name;
JSON Format follow some notations. In Json [] represents list of values or array of values have index in sequence 0 to length of array, {} also contains array of values but here index is called as key and these keys are any kind of string or random number.
Here in question "itemId":["123"],"title":["ebay"] values of both itemid and title are list of values. So while accessing you need to specify which value you need to display. like itemId[0] which return first value. In case no list of values you can directly access it using itemId.
Yes you can combine 2 array and display together. It depends on your logic of combine.
Example:-
Etag = [{"listing_id":123,"title":"etsy","..."}]
Access It as:-
Etag[0].listing_id #123
Etag[0].title #etsy
Etag = [{"listing_id":[123],"title":["etsy"],"..."}]
Access It as:-
Etag[0].listing_id[0] #123
Etag[0].title[0] #etsy
Related
I have the mapping of abbreviation and full name as follow:
object = {"TSLA":"TESLA INC.","GOOG":"GOOGLE INC.","APPL":"APPLE INC.","AMZN":"AMAZON CORPORATION", "MSFT":"MICROSOFT CORPORATION"}
as an output of function i get the following array:
array = ["AMZN","APPL"]
I want to display the values of the associated keys as follows:
output_array = ["AMAZON CORPORATION", "APPLE INC."]
Note that, the actual object has more key: value pairs
Using IndexOf() I can replace one value. However, I believe there must be a way of mapping the array elements to replace the values at once.
Since I am new to JS, I would appreciate any suggestion.
array.map(abbr => object[abbr]) does the job; it iterates over the array rather than the object, which is more efficient as the object already is a lookup table with the right keys - you are right, there is no need to use indexOf which uses a linear search.
Im using fetch on a Rest endpoint which gives me a array of objects. All the keys in the array has a dot in them. ex: {test.name: "test"}. I have to keep fetching new responses to get changes so while i can remove or replace the dot this will take some time/resources every time. Is there any way to use keys with dots in fuse.js?
I tried some variants of this with no luck.
const fuse = new Fuse(this.state.test, {
keys: ['test.name']
});
ps. I cant change the keys in the Rest as its external
As of Fuse.js v6.3.0 you can search through nested values by providing the path via dot (.) or array notation. Therefore, if your key already has a dot in it, you can wrap it in an array:
const fuse = new Fuse(this.state.test, {
// `test.name` is the actual key, so wrap it in an array
keys: [['test.name']]
});
I am trying to make a if statement, I need to determine if the nested array has one array. The objects inside removeByNames is what I need to count. Im using angularJS and linq.js. which ever is more efficient.
[{
"style":"smooth",
"color":"blue",
"data":[[600,40000]],
"name":"Subject Property",
"removeByNames":[["Product1"]],
"$$hashKey":"object:30"
}]
You can use array access and dot notation.
var jsoN = [{"style":"smooth","color":"blue","data":[[600,40000]],"name":"Subject Property","removeByNames":[["Product1"]],"$$hashKey":"object:30"}];
jsoN[0].removeByNames.length;
/* or */
jsoN[0]['removeByNames'].length;
I am running the following piece of code:
var arr = [];
arr["aaa"] = {
"xxx" : 1,
"ttt" : 2
};
arr["bbb"] = {
"xxx" : 5,
"qqq" : 6
};
var tmp = JSON.stringify(arr);
alert(tmp);
But the result is []. How does one stringify an array with string keys and object values?
Use
var arr = {};
Arrays should only be used for numerically indexed data, not arbitrary properties. Of course you are able to do it, because they are, in fact, objects. But it won't work in JSON.
Instead, just use objects.
You can't do that, for two reasons:
The stringify method only consider the data in the array, not properties.
There is no JSON format for an array with properties.
If you want the representation of an array in the JSON, you need to put the data in the actual array, not as properties in the array object.
If you want the properties in the JSON, you need to use a plain object instead of an array.
I have an object which contains some key/value pairs. When there is a key/value pair that shares the same key as another key/value pair, the first one is not recognised when I console log the object.
For example:
var test = {
"same" : 'Value1',
"same" : 'Value2',
"different" : 'Value3'
};
console.log(test);
Results in the console as:
Object { same="Value2", different="Value3"}
Is it not possible to read an object that has similar key names?
I am trying to loop through the object using this method (How do I loop through or enumerate a JavaScript object?) but I can only ever retrieve one the key/value pairs that share a key.
An object can not have duplicate keys.
So the reason that you can't read the duplicate keys from the object, is that they were never added as two items in the object in the first place. One of the items will simply overwrite the other.
Could you change the structure of the JSON if needed? JSON objects cannot have duplicate keys. Think of it as a hashmap or dictionary. Depending on the language and JSON parser you may also get an exception (not in Javascript though)
In your example above either change it so that you have unique keys or change ti to an array of values like:
var test = {
"same" : ['Value1', 'Value2']
"different" : 'Value3'
};
console.log(test);
A key is a unique value that uniquely identifies the element within the array/object. So, the answer is no, you can't have two elements with the same key value.