Angularjs - How to traverse, add and delete elements in nested arrays - javascript

I have a nested array:
$scope.audits = [{
name: '2015',
sections:[{
section:'mm',
id:'1',
subsections: [{
subsection:'aa',
mainelements: ['cc','dd']
},{
subsection:'bb',
mainelements: ['ee','ff','gg']
}]
}]
}];
I want to have a few buttons to add and delete nested elements at will. I cannot use $index because it is multiple layers of array.
Here is my Plunker

This problem got it's crazy solutions out here: AngularJS: traversing nested arrays . I'm not going to rewrite the solutions once again because it's unefficient. Don't forget to use search before asking a question.

Related

Tree from array of objects with dot seperated property

Hello I have an array of objects like this:
data
groups are separated to maximum of 3 levels like: Baseline.name.name.
and i would like to create a tree structure of nested objects. Like this:
[
{
title: "Baseline",
key: "root",
children: [**...NESTED CHILDREN HERE WITH THE SAME STRUCTURE AS ROOT**],
},
];
I need this for a ant design tree component: https://ant.design/components/tree
How do I do this please???
I tried multiple approaches most of them from answers on stack overflow but i didnt manage to get the exact structure i want.

lodash: create groups from array of properties in array of objects

I've recently been using lodash and I just love it. So I'm on a quest to learn it better.
For the past two days I've been trying to figure out how to do the following, without much success: considering an array of objects each with an array of objects in a property props...
let items = [{
name: 'first',
props: [{
id: 1,
name: 'one'
}, {
id: 2,
name: 'two'
}]
}, {
name: 'second',
props: [{
id: 2,
name: 'two'
}]
}, {
name: 'third',
props: [{
id: 1,
name: 'one'
}]
}];
... I'd like to create an array of objects containing each property in props across all items, each having an items property with all items containing that property, like this:
[{
id: 1,
name: 'one',
items : [] // array containing objects `first` and 'third'
},{
id: 2,
name: 'two',
items: [] // array containing objects `first` and 'second'
}]
I've tried various combinations inspired from answers of this question, which seems the closest I could find to what I'm trying to do.I think my real blocker is that, when trying to create an array of unique props (to which I'm going to map the items) and trying to use...
_(items).chain().flatten().pluck('props').unique().value()
... as in this answer, it tells me .chain(...).flatten(...).pluck is not a function. I also tried with .map instead of .pluck, but I guess not everything working in Underscore works in Lodash (I don't know the specific differences, I just read Lodash was forked from Underscore initially).
Note I know how to do this with classic fors and this is only an exercise. I don't have a practical use for it.
I'm just trying to learn to use lodash to its full potential. If you know how to do it, you don't even have to explain. I'll console.log() each step of the way until I understand what's going on. Also, if you think my question's name could be improved, for better indexing, considering what I want to achieve, I'm open to suggestions.
Ideally, the answer should be a chain of lodash methods, without declaring intermediary variables. I'm pretty sure it's doable (that's why I'm asking). Of course, the fewer steps the better — I assume it would impact performance in a real life scenario, but this is not a concern here — I'm aware that most times, when going for performance, the classic for loop is hard to beat.
First, let's transform each item to an array of props with the item's name, flatten this array of arrays, then the rest should be easier.
_(data)
.chain()
.flatMap(({ props, name }) => _.map(props, p => ({
id: p.id,
name: p.name,
item: name
})))
.groupBy(p => p.id)
.map(props => ({
id: _.head(props).id,
name: _.head(props).name,
items: _.map(props, p => p.item)
}))
.value();

Getting a value from the 2 dimensional array in Javascript

In this program I am supposed to create an 2 dimensional array such as ["S1","S2","S3","S4"] AND ["John","Ben","Dan","Jim"] and give the name as output when the specified serial no is given as input. Eg. John will be the output of S1.I was able to create the program using objects but I am unable to do it with arrays. I dont know how to create a 2 dimensional array as well. Kindly help.
Thanks.
Assuming you mean nested arrays and the result you are after is:
[ [ 'S1', 'S2', 'S3', 'S4' ], [ 'John', 'Ben', 'Dan', 'Jim' ] ]
Consider the following:
var mainArray = [];
var arr1 = ["S1","S2","S3","S4"];
var arr2 = ["John","Ben","Dan","Jim"];
mainArray.push(arr1, arr2);
This should give you the result you are after. Please keep in mind that your question is a bit vague and doesn't tell us what you have tried. It sounds like you need some practice with basic JavaScript. I suggest finding tutorials online(which there are more than enough) and working through them.
For future reference, be sure to show what you have tried in your question.

Is multi dimensional array what I am looking for?

It will seems basic stuff for most of you guys but I'm stuck on this problem:
So I have 3 categories of fields like this:
-Category 1 = [field1, field2, field3.. etc]
-Category 2 = [field1, field2, field3.. etc]
-Category 3 = [field1, field2, field3.. etc]
And on top of that, I have others elements which has all of those categories but not all fields like this:
Element 1 = Category 1[field3, field2], Category2[Field4], Category3[field1, field5, field2]
How am I supposed to organize those data in javascript (I am using Jquery if it can help)?
Many programming languages support arrays with named indexes.
Arrays with named indexes are called associative arrays (or hashes).
JavaScript does NOT support arrays with named indexes.
In JavaScript, arrays always use numbered indexes.
You may use objects instead:
{
"Element1": {
"Category1": [
"field3",
"field2"
],
"Category2": [
"field4"
],
"Category3": [
"field1",
"field5",
"field2"
]
}
}
You can take a look at MDN is you are a complete beginner to JavaScript and it's data types.
Eloquent Javascript might help too in getting the basics of javascript.
It depends on what you are trying to do with the data but one way could be:
var category1 = ['field1', 'field2', ... ];
var element1 = {
category1: ['field3', 'field2'],
...
}

complicated custom sorting in nested array

I've problem sorting my nested array, says I've json like this
var orders = [{
'orderId': 1,
'sales': [{
'salesNumbers': 3
}]
}, {
'orderId': 2,
'sales': [{
'salesNumbers': 4
}]
}];
and I wish I can sort orderId base on salesNumbers. You may say it's impossible or I made a mistake by putting sales as array but it contain only 1 object which is salesNumbers. That's not a mistake, I just do not want to simplify my problem.
so it's possible to, without changing the data structure, sort orderId base on salesNumbers?? My app demo http://jsfiddle.net/sq2C3/
Since you say the sales array only has one item in it, you can order by salesNumbers like this:
orderBy:'sales[0].salesNumbers'
Here is an update of your fiddle: http://jsfiddle.net/wittwerj/sq2C3/2/

Categories

Resources