Grouping nested dada - javascript

Say you have csv data file like this:
name, age
Bob, 27
George, 25
Bill, 22
Henry,27
Carol,25
Mary, 28
Harold,27
Jane, 25
I want to aggregate totals by age for a bar chart. So that I get totals like this: age 27(3), age 25(2), etc.
I am using d3 v4

As mentioned in the comments, you could use d3.nest:
const data = [
{name: 'Bob', age: 27},
{name: 'George', age: 25},
{name: 'Bill', age: 2},
{name: 'Henry', age: 27},
{name: 'Carol', age: 25},
{name: 'Mary', age: 28},
{name: 'Harold', age: 27},
{name: 'Jane', age: 25},
];
console.log(d3.nest()
.key(d => d.age)
.rollup(results => results.length)
.entries(data));
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.13.0/d3.min.js"></script>

Related

How can i find all values in array of Docs which are equal or bigger than value

I have an array which has docs like this
[{name: "Tom", age: 20}, {name: "Frank", age: 25}, {name: "Susie", age: 29}, {name: "Tina", age: 32}]
how can i find all people who for example are less or equal then 30 ?
You can use filter()
const arr = [{name: "Tom", age: 20}, {name: "Frank", age: 25}, {name: "Susie", age: 29}, {name: "Tina", age: 32}];
const res = arr.filter(x => x.age <=30)
console.log(res)
If you want the names of the persons only then use map() after filter()
const arr = [{name: "Tom", age: 20}, {name: "Frank", age: 25}, {name: "Susie", age: 29}, {name: "Tina", age: 32}];
const res = arr.filter(x => x.age <=30).map(x => x.name)
console.log(res)
Use Object.entries to key a nested with index of element [index,value] then use filter() on that and at the end map() to get only index
const arr = [{name: "Tom", age: 20}, {name: "Frank", age: 25}, {name: "Susie", age: 29}, {name: "Tina", age: 32}];
const res = Object.entries(arr).filter(([_,v]) => v.age <=30).map(x => +x[0])
console.log(res)
Use Array.filter
let arr =[{name: "Tom", age: 20}, {name: "Frank", age: 25}, {name: "Susie", age: 29}, {name: "Tina", age: 32}];
let result = arr.filter(v => v.age <= 30);
console.log(result);
Yeh filter it is - and with some exagaration in readability:
const dox = [{name: "Tom", age: 20}, {name: "Frank", age: 25}, {name: "Susie", age: 29}, {name: "Tina", age: 32}]
const arrayWithPeopleWhoForExampleAreLessOrEqualThen30 =
dox.filter(peopleWhoForExampleAreLessOrEqualThen30)
function peopleWhoForExampleAreLessOrEqualThen30(doc){
return doc.age <= 30
}

How to output an array in Javascript like a social media feed?

I'm using React for my application and I've put my data (objects) into an array. Is there a way to output my data (array of objects) that loads all at once (such as Twitter, Instagram, Facebook)?
Currently I'm using a for loop where it loads one by one from my latest post to the end.
Here's a sample for loop to demonstrate.
var myArray = [
{name: 'Dwayne', age: 28},
{name: 'Rob', age: 32},
{name: 'Marie', age: 22},
{name: 'Sarah', age: 40},
{name: 'Emma', age: 29},
{name: 'James', age: 30}
];
for (var i = myArray.length - 1; i >= 0; i--){
console.log(myArray[i].name, myArray[i].age);
}
Here's an example of using map to generate a <p/> element with name and age inside it.
render(){
const myArray = [
{name: 'Dwayne', age: 28},
{name: 'Rob', age: 32},
{name: 'Marie', age: 22},
{name: 'Sarah', age: 40},
{name: 'Emma', age: 29},
{name: 'James', age: 30}
];
return(
<div>
{myArray.map((item, index) => (
<p key={`${item.name}-${index}`}>
Name:{item.name}, Age:{item.age}
</p>
))}
</div>
)
}
The above code would output
<div>
<p>Name:Dwayne, Age:28</p>
<p>Name:Rob, Age:32</p>
<p>Name:Marie, Age:22</p>
<p>Name:Sarah, Age:40</p>
<p>Name:Emma, Age:29</p>
<p>Name:James, Age:30</p>
</div>

Using lodash and need to find names in an array that have an age less than X

so I've got this Array
var gillFamily = [{name: 'john', age: 20},
{name: 'richard', age: 27},
{name: 'debbie', age: 55},
{name: 'dan', age 25},
{name: 'robin', age 60}]
And using lodash need to find all the people with an age less than 50.
I tried this _.map to see if it would work
_.map(gillFamily, (el) => el.name, (el) => el.age < 50);
and some filter and reduce stuff with functions but cant get it, Thanks.
_.map() transforms an array. It does not remove elements. For example, if your input array is
const family = [
{name: 'john', age: 20},
{name: 'richard', age: 27},
{name: 'debbie', age: 55},
{name: 'dan', age: 25},
{name: 'robin', age: 60}
]
you could use it to transform it into an array containing descriptions of the persons:
_.map(family, p => p.name + " is " + p.age + " years old")
You'll end up with:
[
'john is 20 years old',
'richard is 27 years old',
'debbie is 55 years old',
'dan is 25 years old',
'robin is 60 years old'
]
To get an array containing the persons younger than 50, one way would be to use _.filter():
_.filter(family, p => p.age < 50)
However, this isn't the only way to do it. There are a number of other ways to achieve the same thing using lodash, and ES6 also provides map() and filter() natively.
You need to make use of the filter function. filter function iterates over elements of collection, returning an array of all elements where the condition is met. More information here.
map is used to transform the array from one form to another which doesn't meet your requirement.
var gillFamily = [{name: 'john', age: 20},
{name: 'richard', age: 27},
{name: 'debbie', age: 55},
{name: 'dan', age: 25},
{name: 'robin', age: 60}];
var ageLessThan50 = _.filter(gillFamily, person => person.age < 50);
console.log(ageLessThan50);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

Remove an attribute from all the rows of JSON object using jquery?

Any idea how to remove an attribute in all the rows of JSON object using Jquery?
Eg:
[{name: "Moroni", age: 50, role: 'Administrator'},
{name: "Tiancum", age: 43, role: 'Administrator'},
{name: "Enos", age: 34, role: 'User'}];
to
[{name: "Moroni", role: 'Administrator'},
{name: "Tiancum", role: 'Administrator'},
{name: "Enos", role: 'User'}];
So the age attribute is removed from all the rows.
You need to iterate through all the elements of an array, and delete age property.
var arr = [{name: "Moroni", age: 50, role: 'Administrator'},
{name: "Tiancum", age: 43, role: 'Administrator'},
{name: "Enos", age: 34, role: 'User'}];
for(var i=0;i<arr.length;i++){
delete arr[i].age;
}
DEMO
In any linux shell:
sed 's/age: [0-9]*(, ){0,1}//' SRC_FILE >DST_FILE

Passing java script function result to Angular ng-table

I have Angular ng-table where I load Json data to $data variable and display in the table.
function ngTable(){
var app = angular.module('main', ['ngTable']).
controller('DemoCtrl', function($scope, ngTableParams) {
var data = [{name: "Moroni", age: 50},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34}];
$scope.tableParams = new ngTableParams({
page: 1, // show first page
count: 10 // count per page
}, {
total: data.length, // length of data
getData: function($defer, params) {
$defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}
});
});
}
Additionally I have Java Script function returning some other Json data - function is called on button click.
function ReturnJson() {
var json = [];
$('body').on('click','button#test', function(){
var id = $(this).attr("value").val();
json = id;
});
return json;
}
How do I replace var data content with ReturnJson() every time on button click action ?
Simple assignment will work, but remember to reload the grid:
$scope.loadData = function () {
$scope.data = $scope.ReturnJson();
$scope.tableParams.reload();
}
$scope.ReturnJson = function () {
var json = [{name: "bb", age: 200},{name: "aaa", age: 100}];
return json;
}
Here is a working demo: http://plnkr.co/edit/Jw41uCmHGAfjkuoLIQor?p=preview

Categories

Resources