how to count number of arrays in nested array using javascript - javascript

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;

Related

In javascript how to replace elements of an array to it's corresponding values in another object?

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.

Use slice on javascript object to loop for all elements except first two?

I have an object of objects and I'd like to use a v-for loop to llop through all the objects except the first two ones, sadly I can't use slice sice it's only for arrays, is it possible to remove the first wo elements of an object using javascript without creating a new object
My object is something like:
{
First: { },
Second: { },
Third: { }
}
I am not that pro in js but first check this url
How to loop through a plain JavaScript object with the objects as members?
this just to get maybe an idea
How can I slice an object in Javascript?
so I will give you a logic where you may get a solution
if you won't get answer from above
when finished from url and get clear understand
create a function where it iterate over objects
from what I suggest
then create a variable =1
if var_inc==1 or var-==2
continue
else
do whatever
then do a for loop to loop over over objects
then do that function
just get the logic maybe you get it..
❤🌷😅
JS objects don't store the order of elements like arrays. In the general case, there is no such thing as order of specific key-value pairs. However, you can iterate through object values using some utility libraries (like underscore https://underscorejs.org/#pairs), or you could just use raw js to do something this:
// this will convert your object to an array of values with arbitrary order
Object.keys(obj).map(key => obj[key])
// this will sort keys alphabetically
Object.keys(obj).sort().map(key => obj[key])
Note that some browsers can retain order of keys when calling Object.keys() but you should not rely on it, because it isn't guaranteed.
I would suggest to just use array of objects to be sure of order like this:
[{ key: "First", value: 1 }, { key: "Second", value: 2}]
If you just want to delete the properties of the objects then you can use delete keyword.
delete Obj['First']
delete Obj['Second']`
This would delete both the keys and object would have only 'Third' key

Why does some JSON arrays have brackets in values?

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

How can I manually access a third-dimension javascript Array

I've been trying to access a third level node in an array using the indexes in it, but I can't access it, I tried a lot of ways that I found here on SO but I don't want to iterate through it, I want to get it manually.
var data = [
{code:1,
label:'John Doe',
tasks:[{
code:1,
label: 'AnyProject',
starts:'2016/1/25',
ends:'2016/2/25'}]
}];
What I want to do (theoretically):
data[0].tasks.code
data[0].tasks[0].code
tasks is an Array so you need to access it like an array.
data[0].tasks[0].code
Inside data array you have tasks and inside task array you have property code.
[] is an array you can use index to look inside.
{} is an object you can access using .

Joining values in an Javascript array

Say I have:
var Certificated = {}
Sub items are added dynamically and variate. Possible outcome:
var Certificated = {
Elementary: ["foo","bar", "ball"]
MiddleSchool: ["bar", "crampapydime"]
};
I want to do the following:
Certificated.Elementary = Certificated.Elementary.join("");
Except I need it to do that on all of the objects inside.
Keep in mind I can't know for sure the titles of nor how many objects will be inside Certificated.
My question is how can I use .join("") on all elements inside Certificated, without calling each one specifically?
EDIT: I am aware .join() is for arrays and the objects inside Certificated are going to be arrays. Therefore the join method.
Does this work?
for (var key in Certificated) {
if (Certificated.hasOwnProperty(key)) {
Certificated[key] = Certificated[key].join("");
}
}
It loops through all properties of Certificated, and makes a quick safe check for the key being a real property, then uses bracket notation - [""] - to do your join.
Quick question - are you sure you want to use join? I know you just provided an example, but you can't call join on a string...it's for arrays. Just wanted to make sure you knew.
Here's a jsFiddle of my code working with arrays being used for the properties:
http://jsfiddle.net/v48dL/
Notice in the browser console, the properties' values are strings because the join combined them with "".

Categories

Resources