Related
data = [
{
"index": 0,
"id": 47,
"sepallengthcm": 5.1,
"sepalwidthcm": 3.8,
"unnamed:_3": 1.6,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 1,
"id": 48,
"sepallengthcm": 4.6,
"sepalwidthcm": 3.2,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 2,
"id": 49,
"sepallengthcm": 5.3,
"sepalwidthcm": 3.7,
"unnamed:_3": 1.5,
"petalwidthcm": 0.2,
"species": "jennifer"
},
{
"index": 3,
"id": 50,
"sepallengthcm": 5.0,
"sepalwidthcm": 3.3,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 4,
"id": 97,
"sepallengthcm": 12.0,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.2,
"petalwidthcm": 1.3,
"species": "jennifer"
},
{
"index": 5,
"id": 98,
"sepallengthcm": 6.2,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.3,
"petalwidthcm": 1.3,
"species": "jennifer"
},
{
"index": 6,
"id": 99,
"sepallengthcm": 5.1,
"sepalwidthcm": 2.5,
"unnamed:_3": 3.0,
"petalwidthcm": 1.1,
"species": "kajol"
},
{
"index": 7,
"id": 100,
"sepallengthcm": 11.0,
"sepalwidthcm": 2.8,
"unnamed:_3": 7.0,
"petalwidthcm": 1.3,
"species": "floaw"
},
{
"index": 8,
"id": 101,
"sepallengthcm": 6.3,
"sepalwidthcm": 3.3,
"unnamed:_3": 6.0,
"petalwidthcm": 2.5,
"species": "Iris-flower"
},
{
"index": 9,
"id": 102,
"sepallengthcm": 5.8,
"sepalwidthcm": 2.7,
"unnamed:_3": 5.1,
"petalwidthcm": 1.9,
"species": "Iris-flower"
}
]
Here is my input data.
I am trying to achive distict count of this data using spcific field
result = distictCount("species")
result = [
{
"species": "Iris-flower",
"sepallengthcm": 5.8,
"sepalwidthcm": 2.7
},
{
"species": "floaw",
"sepallengthcm": 11.0,
"sepalwidthcm": 2.8
},
{
"species": "jennifer",
"sepallengthcm": 5.3,
"sepalwidthcm": 2.9
},
{
"species": "kajol",
"sepallengthcm": 5.1,
"sepalwidthcm": 4.5
},
{
"species": "setosa",
"sepallengthcm": 3.2,
"sepalwidthcm": 2.7
}
]
Above result i am expecting
I am trying to achive minimum value by aggregationg selected fields. for two fields ata time
let GroupMin = (arr, category, value1, value2) => {
let result = Object.values(arr.reduce(function(r, e) {
let key = e[category];
if (!r[key]) r[key] = e;
else {
let first_value = parseFloat(r[key][value1])
let second_value = parseFloat(r[key][value2])
if(parseFloat(e[value1]) > first_value){
first_value = parseFloat(e[value1]);
}
if(parseFloat(e[value2]) > second_value){
second_value = parseFloat(e[value2]);
}
}
return r;
}, {}))
return result
}
GroupMin(data, "setosa", "sepallengthcm", "sepalwidthcm)
My code is not working.
Please take a look.
How can we do that
Thanks
You could first group by a key.
After you have your groups, you can reduce the values and map the fields to the minimum values.
const key = 'species';
const fields = [ 'species', 'sepallengthcm', 'sepalwidthcm' ];
const main = () => {
fetchData().then(data =>
console.log(distictCount(data, key, fields)));
};
const distictCount = (data, key, fields) => {
const groups = data.reduce((result, data) => ({
...result,
[data[key]]: [ ...(result[data[key]] || []), data ]
}), {});
const values = Object.values(groups).map(group =>
fields.reduce((res, field) => ({
...res,
[field]: field === key
? group[0][key]
: Math.min(...group.map(obj => obj[field]))
}), {}));
return values.sort((a, b) =>
a[key].localeCompare(b[key], 'en', { sensitivity: 'base' }));
};
const fetchData = () => Promise.resolve([{
"index": 0,
"id": 47,
"sepallengthcm": 5.1,
"sepalwidthcm": 3.8,
"unnamed:_3": 1.6,
"petalwidthcm": 0.2,
"species": "setosa"
}, {
"index": 1,
"id": 48,
"sepallengthcm": 4.6,
"sepalwidthcm": 3.2,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
}, {
"index": 2,
"id": 49,
"sepallengthcm": 5.3,
"sepalwidthcm": 3.7,
"unnamed:_3": 1.5,
"petalwidthcm": 0.2,
"species": "jennifer"
}, {
"index": 3,
"id": 50,
"sepallengthcm": 5.0,
"sepalwidthcm": 3.3,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
}, {
"index": 4,
"id": 97,
"sepallengthcm": 12.0,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.2,
"petalwidthcm": 1.3,
"species": "jennifer"
}, {
"index": 5,
"id": 98,
"sepallengthcm": 6.2,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.3,
"petalwidthcm": 1.3,
"species": "jennifer"
}, {
"index": 6,
"id": 99,
"sepallengthcm": 5.1,
"sepalwidthcm": 2.5,
"unnamed:_3": 3.0,
"petalwidthcm": 1.1,
"species": "kajol"
}, {
"index": 7,
"id": 100,
"sepallengthcm": 11.0,
"sepalwidthcm": 2.8,
"unnamed:_3": 7.0,
"petalwidthcm": 1.3,
"species": "floaw"
}, {
"index": 8,
"id": 101,
"sepallengthcm": 6.3,
"sepalwidthcm": 3.3,
"unnamed:_3": 6.0,
"petalwidthcm": 2.5,
"species": "Iris-flower"
}, {
"index": 9,
"id": 102,
"sepallengthcm": 5.8,
"sepalwidthcm": 2.7,
"unnamed:_3": 5.1,
"petalwidthcm": 1.9,
"species": "Iris-flower"
}]);
main();
.as-console-wrapper { top: 0; max-height: 100% !important; }
data = [
{
"index": 0,
"id": 47,
"sepallengthcm": 5.1,
"sepalwidthcm": 3.8,
"unnamed:_3": 1.6,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 1,
"id": 48,
"sepallengthcm": 4.6,
"sepalwidthcm": 3.2,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 2,
"id": 49,
"sepallengthcm": 5.3,
"sepalwidthcm": 3.7,
"unnamed:_3": 1.5,
"petalwidthcm": 0.2,
"species": "jennifer"
},
{
"index": 3,
"id": 50,
"sepallengthcm": 5.0,
"sepalwidthcm": 3.3,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 4,
"id": 97,
"sepallengthcm": 12.0,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.2,
"petalwidthcm": 1.3,
"species": "jennifer"
},
{
"index": 5,
"id": 98,
"sepallengthcm": 6.2,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.3,
"petalwidthcm": 1.3,
"species": "jennifer"
},
{
"index": 6,
"id": 99,
"sepallengthcm": 5.1,
"sepalwidthcm": 2.5,
"unnamed:_3": 3.0,
"petalwidthcm": 1.1,
"species": "kajol"
},
{
"index": 7,
"id": 100,
"sepallengthcm": 11.0,
"sepalwidthcm": 2.8,
"unnamed:_3": 7.0,
"petalwidthcm": 1.3,
"species": "floaw"
},
{
"index": 8,
"id": 101,
"sepallengthcm": 6.3,
"sepalwidthcm": 3.3,
"unnamed:_3": 6.0,
"petalwidthcm": 2.5,
"species": "Iris-flower"
},
{
"index": 9,
"id": 102,
"sepallengthcm": 5.8,
"sepalwidthcm": 2.7,
"unnamed:_3": 5.1,
"petalwidthcm": 1.9,
"species": "Iris-flower"
}
]
Here is my input
export const groupByMultiple = (arr, category, value1, value2) => {
let result = Object.values(arr.reduce(function(r, e) {
let key = e[category];
if (!r[key]) r[key] = e;
else {
let first_value = parseFloat(r[key][value1])
let second_value = parseFloat(r[key][value2])
first_value += parseFloat(e[value1]);
second_value += parseFloat(e[value2])
}
return r;
}, {}))
return result
}
groupByMultiple(data, "species", "sepallengthcm", "sepalwidthcm")
Above function gives sum by aggregating species and summing sepallengthcm and sepalwidthcm.
Same way i am trying to achieve avg and count. But, unfortunately my methods are not working.
Please take a look
Thanks..
Above function gives sum by aggregating species and summing sepallengthcm and sepalwidthcm.
Same way i am trying to achieve avg and count. But, unfortunately my methods are not working.
Please take a look
Thanks..
You can add new key count for each key in the reduce function. And then with the result array you can call .map() and calculate the average for value1 and value2 and add them as key in the object and then return that object,
data = [
{
"index": 0,
"id": 47,
"sepallengthcm": 5.1,
"sepalwidthcm": 3.8,
"unnamed:_3": 1.6,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 1,
"id": 48,
"sepallengthcm": 4.6,
"sepalwidthcm": 3.2,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 2,
"id": 49,
"sepallengthcm": 5.3,
"sepalwidthcm": 3.7,
"unnamed:_3": 1.5,
"petalwidthcm": 0.2,
"species": "jennifer"
},
{
"index": 3,
"id": 50,
"sepallengthcm": 5.0,
"sepalwidthcm": 3.3,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 4,
"id": 97,
"sepallengthcm": 12.0,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.2,
"petalwidthcm": 1.3,
"species": "jennifer"
},
{
"index": 5,
"id": 98,
"sepallengthcm": 6.2,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.3,
"petalwidthcm": 1.3,
"species": "jennifer"
},
{
"index": 6,
"id": 99,
"sepallengthcm": 5.1,
"sepalwidthcm": 2.5,
"unnamed:_3": 3.0,
"petalwidthcm": 1.1,
"species": "kajol"
},
{
"index": 7,
"id": 100,
"sepallengthcm": 11.0,
"sepalwidthcm": 2.8,
"unnamed:_3": 7.0,
"petalwidthcm": 1.3,
"species": "floaw"
},
{
"index": 8,
"id": 101,
"sepallengthcm": 6.3,
"sepalwidthcm": 3.3,
"unnamed:_3": 6.0,
"petalwidthcm": 2.5,
"species": "Iris-flower"
},
{
"index": 9,
"id": 102,
"sepallengthcm": 5.8,
"sepalwidthcm": 2.7,
"unnamed:_3": 5.1,
"petalwidthcm": 1.9,
"species": "Iris-flower"
}
]
const groupByMultiple = (arr, category, value1, value2) => {
let result = Object.values(arr.reduce(function(r, e) {
let key = e[category];
if (!r[key]) r[key] = {...e, count: 1};
else {
let first_value = parseFloat(r[key][value1])
let second_value = parseFloat(r[key][value2])
first_value += parseFloat(e[value1]);
second_value += parseFloat(e[value2])
r[key][value1] = first_value;
r[key][value2] = second_value;
r[key]['count'] += 1;
}
return r;
}, {}));
return result.map(item => {
item[`average_${value1}`] = item[value1]/item.count;
item[`average_${value2}`] = item[value2]/item.count;
return item;
})
}
const ret = groupByMultiple(data, "species", "sepallengthcm", "sepalwidthcm");
console.log(ret);
I have a JSON Object as show below.
response = [{
"Myanmar": [{
"EDCBA0000013620": {
"mou": 0.0,
"CA": 1.0,
"CCS": 0.0,
"COC": 0.0
}
}],
"Gibraltar": [{
"ABCDE0000013643": {
"mou": 12.850000381469727,
"CA": 1.0,
"CCS": 1.0,
"COC": 3.0
}
}],
"Cyprus": [{
"ABCDE0000010121": {
"mou": 36.25,
"CA": 3.0,
"CCS": 2.0,
"COC": 7.0
},
"ABCDE0000013643": {
"mou": 27.299999237060547,
"CA": 1.0,
"CCS": 1.0,
"COC": 6.0
},
"ABCDE0000013662": {
"mou": 80.59999752044678,
"CA": 4.0,
"CCS": 4.0,
"COC": 14.0
},
"ABCDE0000010328": {
"mou": 26.716670513153076,
"CA": 4.0,
"CCS": 4.0,
"COC": 6.0
}
}],
"Kazakhstan": [{
"EDCBA0000013620": {
"mou": 0.0,
"CA": 32.0,
"CCS": 0.0,
"COC": 0.0
},
"ABCDE0000013643": {
"mou": 17.0,
"CA": 1.0,
"CCS": 1.0,
"COC": 3.0
},
"ABCDE0000010121": {
"mou": 15.783329963684082,
"CA": 1.0,
"CCS": 1.0,
"COC": 4.0
},
"EDCBA0000015450": {
"mou": 11.683329582214355,
"CA": 23.0,
"CCS": 1.0,
"COC": 3.0
},
"ABCDE0000010328": {
"mou": 0.0,
"CA": 0.0,
"CCS": 0.0,
"COC": 4.0
},
"EDCBA0000015451": {
"mou": 11.316670417785645,
"CA": 29.0,
"CCS": 1.0,
"COC": 2.0
},
"EDCBA0000010541": {
"mou": 17.316669464111328,
"CA": 30.0,
"CCS": 1.0,
"COC": 3.0
}
}],
"Portugal": [{
"ABCDE0000013643": {
"mou": 352.2333300113678,
"CA": 30.0,
"CCS": 30.0,
"COC": 67.0
},
"ABCDE0000010121": {
"mou": 342.4499905705452,
"CA": 25.0,
"CCS": 24.0,
"COC": 65.0
},
"EDCBA0000013620": {
"mou": 85.1666567698121,
"CA": 3.0,
"CCS": 3.0,
"COC": 19.0
},
"ABCDE0000013662": {
"mou": 478.6499952673912,
"CA": 26.0,
"CCS": 26.0,
"COC": 92.0
},
"ABCDE0000010328": {
"mou": 347.5833450257778,
"CA": 25.0,
"CCS": 25.0,
"COC": 57.0
},
"EDCBA0000015450": {
"mou": 15.883330345153809,
"CA": 1.0,
"CCS": 1.0,
"COC": 4.0
},
"EDCBA0000055797": {
"mou": 31.799999237060547,
"CA": 2.0,
"CCS": 2.0,
"COC": 4.0
},
"EDCBA0000015451": {
"mou": 9.150000035762787,
"CA": 3.0,
"CCS": 2.0,
"COC": 1.0
},
"EDCBA0000010541": {
"mou": 57.78332122415304,
"CA": 8.0,
"CCS": 5.0,
"COC": 11.0
}
}],
"Iceland": [{
"ABCDE0000013662": {
"mou": 1.783329963684082,
"CA": 1.0,
"CCS": 1.0,
"COC": 0.0
}
}]
}]
what i want to achieve is group every country data based on the substring of keys inside every-country array.
Expected JSON:
"Cyprus": [
"ABCDE": {
"ABCDE0000010121": {
"mou": 36.25,
"CA": 3.0,
"CCS": 2.0,
"COC": 7.0
},
"ABCDE0000013643": {
"mou": 27.299999237060547,
"CA": 1.0,
"CCS": 1.0,
"COC": 6.0
}
},
"EDCBA": {
"EDCBA0000013662": {
"mou": 80.59999752044678,
"CA": 4.0,
"CCS": 4.0,
"COC": 14.0
},
"EDCBA0000010328": {
"mou": 26.716670513153076,
"CA": 4.0,
"CCS": 4.0,
"COC": 6.0
}
}
]
i tried achieving this using loadash, but didnot succeed.
below is the function i have written using loadash.
res=[
{
"Myanmar": [
{
"EDCBA0000013620": {
"mou": 0.0,
"CA": 1.0,
"CCS": 0.0,
"COC": 0.0
}
}
],
"Gibraltar": [
{
"ABCDE0000013643": {
"mou": 12.850000381469727,
"CA": 1.0,
"CCS": 1.0,
"COC": 3.0
}
}
],
"Cyprus": [
{
"ABCDE0000010121": {
"mou": 36.25,
"CA": 3.0,
"CCS": 2.0,
"COC": 7.0
},
"ABCDE0000013643": {
"mou": 27.299999237060547,
"CA": 1.0,
"CCS": 1.0,
"COC": 6.0
},
"ABCDE0000013662": {
"mou": 80.59999752044678,
"CA": 4.0,
"CCS": 4.0,
"COC": 14.0
},
"ABCDE0000010328": {
"mou": 26.716670513153076,
"CA": 4.0,
"CCS": 4.0,
"COC": 6.0
}
}
],
"Kazakhstan": [
{
"EDCBA0000013620": {
"mou": 0.0,
"CA": 32.0,
"CCS": 0.0,
"COC": 0.0
},
"ABCDE0000013643": {
"mou": 17.0,
"CA": 1.0,
"CCS": 1.0,
"COC": 3.0
},
"ABCDE0000010121": {
"mou": 15.783329963684082,
"CA": 1.0,
"CCS": 1.0,
"COC": 4.0
},
"EDCBA0000015450": {
"mou": 11.683329582214355,
"CA": 23.0,
"CCS": 1.0,
"COC": 3.0
},
"ABCDE0000010328": {
"mou": 0.0,
"CA": 0.0,
"CCS": 0.0,
"COC": 4.0
},
"EDCBA0000015451": {
"mou": 11.316670417785645,
"CA": 29.0,
"CCS": 1.0,
"COC": 2.0
},
"EDCBA0000010541": {
"mou": 17.316669464111328,
"CA": 30.0,
"CCS": 1.0,
"COC": 3.0
}
}
],
"Portugal": [
{
"ABCDE0000013643": {
"mou": 352.2333300113678,
"CA": 30.0,
"CCS": 30.0,
"COC": 67.0
},
"ABCDE0000010121": {
"mou": 342.4499905705452,
"CA": 25.0,
"CCS": 24.0,
"COC": 65.0
},
"EDCBA0000013620": {
"mou": 85.1666567698121,
"CA": 3.0,
"CCS": 3.0,
"COC": 19.0
},
"ABCDE0000013662": {
"mou": 478.6499952673912,
"CA": 26.0,
"CCS": 26.0,
"COC": 92.0
},
"ABCDE0000010328": {
"mou": 347.5833450257778,
"CA": 25.0,
"CCS": 25.0,
"COC": 57.0
},
"EDCBA0000015450": {
"mou": 15.883330345153809,
"CA": 1.0,
"CCS": 1.0,
"COC": 4.0
},
"EDCBA0000055797": {
"mou": 31.799999237060547,
"CA": 2.0,
"CCS": 2.0,
"COC": 4.0
},
"EDCBA0000015451": {
"mou": 9.150000035762787,
"CA": 3.0,
"CCS": 2.0,
"COC": 1.0
},
"EDCBA0000010541": {
"mou": 57.78332122415304,
"CA": 8.0,
"CCS": 5.0,
"COC": 11.0
}
}
],
"Iceland": [
{
"ABCDE0000013662": {
"mou": 1.783329963684082,
"CA": 1.0,
"CCS": 1.0,
"COC": 0.0
}
}
]
}
]
var result = _.map(_.flatMap(res));
// console.log(result,"result");
for (let [key, value] of Object.entries(result[0])){
for(let[ikey,ivalue]of Object.entries(value)){
for(let[valueKey,valueArr] of Object.entries(ivalue)){
// console.log(valueKey,valueArr);
valueArr.trunkId = valueKey
valueArr[name] = key;
this.groupedData.push(valueArr);
}
}
}
let result1 = _.chain(this.groupedData)
.groupBy("previewFrame")
.map((value, key) => ({ country: key, trunks: value }))
.value()
console.log(JSON.stringify(result1))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js"></script>
I am able to group the data for one level, but in lodash how to group the array inside a Object using a substring of key.
I am stuck here,
Please help.
Thanks in Advance
You could map the entries and build a new grouping level for the countries.
var data = [{ Myanmar: [{ EDCBA0000013620: { mou: 0, CA: 1, CCS: 0, COC: 0 } }], Gibraltar: [{ ABCDE0000013643: { mou: 12.850000381469727, CA: 1, CCS: 1, COC: 3 } }], Cyprus: [{ ABCDE0000010121: { mou: 36.25, CA: 3, CCS: 2, COC: 7 }, ABCDE0000013643: { mou: 27.299999237060547, CA: 1, CCS: 1, COC: 6 }, ABCDE0000013662: { mou: 80.59999752044678, CA: 4, CCS: 4, COC: 14 }, ABCDE0000010328: { mou: 26.716670513153076, CA: 4, CCS: 4, COC: 6 } }], Kazakhstan: [{ EDCBA0000013620: { mou: 0, CA: 32, CCS: 0, COC: 0 }, ABCDE0000013643: { mou: 17, CA: 1, CCS: 1, COC: 3 }, ABCDE0000010121: { mou: 15.783329963684082, CA: 1, CCS: 1, COC: 4 }, EDCBA0000015450: { mou: 11.683329582214355, CA: 23, CCS: 1, COC: 3 }, ABCDE0000010328: { mou: 0, CA: 0, CCS: 0, COC: 4 }, EDCBA0000015451: { mou: 11.316670417785645, CA: 29, CCS: 1, COC: 2 }, EDCBA0000010541: { mou: 17.316669464111328, CA: 30, CCS: 1, COC: 3 } }], Portugal: [{ ABCDE0000013643: { mou: 352.2333300113678, CA: 30, CCS: 30, COC: 67 }, ABCDE0000010121: { mou: 342.4499905705452, CA: 25, CCS: 24, COC: 65 }, EDCBA0000013620: { mou: 85.1666567698121, CA: 3, CCS: 3, COC: 19 }, ABCDE0000013662: { mou: 478.6499952673912, CA: 26, CCS: 26, COC: 92 }, ABCDE0000010328: { mou: 347.5833450257778, CA: 25, CCS: 25, COC: 57 }, EDCBA0000015450: { mou: 15.883330345153809, CA: 1, CCS: 1, COC: 4 }, EDCBA0000055797: { mou: 31.799999237060547, CA: 2, CCS: 2, COC: 4 }, EDCBA0000015451: { mou: 9.150000035762787, CA: 3, CCS: 2, COC: 1 }, EDCBA0000010541: { mou: 57.78332122415304, CA: 8, CCS: 5, COC: 11 } }], Iceland: [{ ABCDE0000013662: { mou: 1.783329963684082, CA: 1, CCS: 1, COC: 0 } }] }],
result = data.map(o => Object.fromEntries(Object.entries(o).map(([k, v]) => [
k,
v.map(o => Object.entries(o).reduce((r, [l, w]) => {
const key = l.slice(0, 5);
r[key] = r[key] || {};
r[key][l] = w;
return r;
}, {}))
])));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I've created a custom function to compile in one array data from different sheets in Google Sheets.
Here is how it goes:
function COMPILER(){
var i;
var c;
var display = [];
var resultado = [];
for (i = 0; i < arguments.length; i++) {
display.push(arguments[i]);
};
for (c = 0; c < display.length; c++) {
resultado = resultado.concat(display[c]);
};
return resultado;
};
The code runs just fine, but I've ran into an unexpected issue. Since there are hidden formulas in between the data sources, my final array is compiled with several null values. The final matrix looks a little like this:
resultado [[3, f, 2, 6, 0, r], [2, 2, t, 5, 6, 8], **[, , , , , ]**, **[, , , , , ]**, **[, , , , , ]**, [4, y, y, 7, 8, 0], **[, , , , , ]**...]
I have no clue how to correctly (and selectively) remove those empty values from within the matrix. I would assume I'd have to read and test the whole matrix and remove each selected value, everything within a loop, but I couldn't do on my own.
This code will help you with your issue, it takes every array within the main array and then checks if there are null values to put them away and build a new clean array.
function testArray(){
var arrTest = [[3, 'f', 2, 6, 0, 'r'], [2, 2, 't', 5, 6, 8],[, , , , , ],[, , , , , ], [, , , , , ], [4, 'y', 'y', 7, 8, 0], [, , , , , ]];
// Initiate an empty array to fill it with the non empty data from your Array
var cleanArr = [];
// Check every array inside your main array
arrTest.forEach(function(el){
// If there are null values, this will clean them
var filteredArr = el.filter(function(e){ return e != null;});
// If the array ends up empty, don't push it into your clean array
if(filteredArr.length) cleanArr.push(filteredArr);
});
Logger.log(cleanArr);
}
Docs
Due to the fact you are handling a lot of operations with arrays, I would recommend you to check this:
Best Practices.
I modified your function to get a range.getValues() Object[][] on a sheet that I had removed one of the rows out from the middle of the page. I used the Array.some method to removed the empty row.
Below I have included the code and the Logger.log outputs
function function101(){
var result = [];
Logger.log(JSON.stringify(arguments));
for(var i=0;i<arguments[0].length;i++) {
Logger.log(arguments[0][i]);
if(arguments[0][i].some(function(e){return e;})) {
result.push(arguments[0][i]);
}
}
Logger.log(result);
return result;
}
//I ran this function
function testfunction101() {
var vA=SpreadsheetApp.getActiveSheet().getDataRange().getValues();
function101(vA);
}
/*
[19-12-06 17:24:49:234 MST] {"0":[["HDR1","HDR2","HDR3","HDR4","HDR5","HDR6","HDR7","HDR8","HDR9","HDR10"],[6,9,5,16,7,8,3,6,17,18],[14,19,12,17,10,13,0,2,19,16],[7,3,13,11,15,14,5,17,9,6],[11,10,11,11,4,5,18,9,11,9],[12,6,2,6,5,12,1,2,4,9],[3,16,11,19,16,12,0,19,8,1],[3,0,5,8,8,12,16,6,4,18],[8,0,3,4,11,2,3,7,14,18],[11,15,16,8,7,4,15,16,16,0],[15,5,11,18,8,15,8,2,8,16],[12,8,1,12,3,1,12,11,12,19],[10,11,9,4,12,11,19,17,4,0],["","","","","","","","","",""],[2,4,1,5,14,3,14,15,13,11],[3,7,0,0,18,15,16,11,8,7],[19,7,7,17,17,9,15,14,15,5],[13,2,19,19,8,15,16,13,16,2],[16,3,16,13,13,15,9,1,17,15],[9,1,4,8,3,1,9,8,19,5],[7,13,17,9,6,2,18,1,3,16]]}
[19-12-06 17:24:49:235 MST] [HDR1, HDR2, HDR3, HDR4, HDR5, HDR6, HDR7, HDR8, HDR9, HDR10]
[19-12-06 17:24:49:236 MST] [6.0, 9.0, 5.0, 16.0, 7.0, 8.0, 3.0, 6.0, 17.0, 18.0]
[19-12-06 17:24:49:237 MST] [14.0, 19.0, 12.0, 17.0, 10.0, 13.0, 0.0, 2.0, 19.0, 16.0]
[19-12-06 17:24:49:238 MST] [7.0, 3.0, 13.0, 11.0, 15.0, 14.0, 5.0, 17.0, 9.0, 6.0]
[19-12-06 17:24:49:239 MST] [11.0, 10.0, 11.0, 11.0, 4.0, 5.0, 18.0, 9.0, 11.0, 9.0]
[19-12-06 17:24:49:239 MST] [12.0, 6.0, 2.0, 6.0, 5.0, 12.0, 1.0, 2.0, 4.0, 9.0]
[19-12-06 17:24:49:240 MST] [3.0, 16.0, 11.0, 19.0, 16.0, 12.0, 0.0, 19.0, 8.0, 1.0]
[19-12-06 17:24:49:241 MST] [3.0, 0.0, 5.0, 8.0, 8.0, 12.0, 16.0, 6.0, 4.0, 18.0]
[19-12-06 17:24:49:242 MST] [8.0, 0.0, 3.0, 4.0, 11.0, 2.0, 3.0, 7.0, 14.0, 18.0]
[19-12-06 17:24:49:242 MST] [11.0, 15.0, 16.0, 8.0, 7.0, 4.0, 15.0, 16.0, 16.0, 0.0]
[19-12-06 17:24:49:243 MST] [15.0, 5.0, 11.0, 18.0, 8.0, 15.0, 8.0, 2.0, 8.0, 16.0]
[19-12-06 17:24:49:243 MST] [12.0, 8.0, 1.0, 12.0, 3.0, 1.0, 12.0, 11.0, 12.0, 19.0]
[19-12-06 17:24:49:244 MST] [10.0, 11.0, 9.0, 4.0, 12.0, 11.0, 19.0, 17.0, 4.0, 0.0]
[19-12-06 17:24:49:244 MST] [, , , , , , , , , ]
[19-12-06 17:24:49:245 MST] [2.0, 4.0, 1.0, 5.0, 14.0, 3.0, 14.0, 15.0, 13.0, 11.0]
[19-12-06 17:24:49:246 MST] [3.0, 7.0, 0.0, 0.0, 18.0, 15.0, 16.0, 11.0, 8.0, 7.0]
[19-12-06 17:24:49:246 MST] [19.0, 7.0, 7.0, 17.0, 17.0, 9.0, 15.0, 14.0, 15.0, 5.0]
[19-12-06 17:24:49:247 MST] [13.0, 2.0, 19.0, 19.0, 8.0, 15.0, 16.0, 13.0, 16.0, 2.0]
[19-12-06 17:24:49:248 MST] [16.0, 3.0, 16.0, 13.0, 13.0, 15.0, 9.0, 1.0, 17.0, 15.0]
[19-12-06 17:24:49:249 MST] [9.0, 1.0, 4.0, 8.0, 3.0, 1.0, 9.0, 8.0, 19.0, 5.0]
[19-12-06 17:24:49:249 MST] [7.0, 13.0, 17.0, 9.0, 6.0, 2.0, 18.0, 1.0, 3.0, 16.0]
[19-12-06 17:24:49:250 MST] [[HDR1, HDR2, HDR3, HDR4, HDR5, HDR6, HDR7, HDR8, HDR9, HDR10], [6.0, 9.0, 5.0, 16.0, 7.0, 8.0, 3.0, 6.0, 17.0, 18.0], [14.0, 19.0, 12.0, 17.0, 10.0, 13.0, 0.0, 2.0, 19.0, 16.0], [7.0, 3.0, 13.0, 11.0, 15.0, 14.0, 5.0, 17.0, 9.0, 6.0], [11.0, 10.0, 11.0, 11.0, 4.0, 5.0, 18.0, 9.0, 11.0, 9.0], [12.0, 6.0, 2.0, 6.0, 5.0, 12.0, 1.0, 2.0, 4.0, 9.0], [3.0, 16.0, 11.0, 19.0, 16.0, 12.0, 0.0, 19.0, 8.0, 1.0], [3.0, 0.0, 5.0, 8.0, 8.0, 12.0, 16.0, 6.0, 4.0, 18.0], [8.0, 0.0, 3.0, 4.0, 11.0, 2.0, 3.0, 7.0, 14.0, 18.0], [11.0, 15.0, 16.0, 8.0, 7.0, 4.0, 15.0, 16.0, 16.0, 0.0], [15.0, 5.0, 11.0, 18.0, 8.0, 15.0, 8.0, 2.0, 8.0, 16.0], [12.0, 8.0, 1.0, 12.0, 3.0, 1.0, 12.0, 11.0, 12.0, 19.0], [10.0, 11.0, 9.0, 4.0, 12.0, 11.0, 19.0, 17.0, 4.0, 0.0], [2.0, 4.0, 1.0, 5.0, 14.0, 3.0, 14.0, 15.0, 13.0, 11.0], [3.0, 7.0, 0.0, 0.0, 18.0, 15.0, 16.0, 11.0, 8.0, 7.0], [19.0, 7.0, 7.0, 17.0, 17.0, 9.0, 15.0, 14.0, 15.0, 5.0], [13.0, 2.0, 19.0, 19.0, 8.0, 15.0, 16.0, 13.0, 16.0, 2.0], [16.0, 3.0, 16.0, 13.0, 13.0, 15.0, 9.0, 1.0, 17.0, 15.0], [9.0, 1.0, 4.0, 8.0, 3.0, 1.0, 9.0, 8.0, 19.0, 5.0], [7.0, 13.0, 17.0, 9.0, 6.0, 2.0, 18.0, 1.0, 3.0, 16.0]]
I'm not sure if this was your problem but you said I've created a custom function to compile in one array data from different sheets in Google Sheets. and my input data was gathered from a sheet.getDataRange() method.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am a beginner in coding and currently I'm trying to take the information from my json file and create a table in html. I'm using atom code editor and I am trying to get javascript to loop through all the data to make a table through using jQuery. I have tried to pull the information from my json file using jquery countless amounts of times but I seem to be doing something wrong. I've looked through plenty of forums and have tried different codes but none of them seem to work. The Json code is below. Thanks.
[
{
"Season": 2006-2007,
"Team": "P4two Ballers Osnabrueck",
"Games": 32,
"FG%": 65,
"FT%": 71,
"Assists": 4.5,
"Steals": 1.8,
"Blocks": 1.5,
"TRB": 11.9,
"Points Per Game": 16.7
},
{
"Season": 2007-2008,
"Team": "TG Renesas Landshut",
"Games": 26,
"FG%": 67,
"FT%": 68,
"Assists": 5,
"Steals": 1.2,
"Blocks": 0.8,
"TRB": 16.3,
"Points Per Game": 14.5
},
{
"Season": 2008-2009,
"Team": "Head Attack Erding",
"Games": 20,
"FG%": 69,
"FT%": 75,
"Assists": 6.2,
"Steals": 2.3,
"Blocks": 1.2,
"TRB": 17.1,
"Points Per Game": 12.8
},
{
"Season": 2009-2010,
"Team": "Deportivo Espanol de Talca",
"Games": 22,
"FG%": 66.5,
"FT%": 71,
"Assists": 4.1,
"Steals": 2,
"Blocks": 2,
"TRB": 11.6,
"Points Per Game": 16.8
},
{
"Season": 2011,
"Team": "Club Trouville Montevideo",
"Games": 8,
"FG%": 65,
"FT%": 75,
"Assists": 4.8,
"Steals": 3,
"Blocks": 1.8,
"TRB": 15,
"Points Per Game": 25
},
{
"Season": 2011-2012,
"Team": "San Isidro San Francisco",
"Games": 54,
"FG%": 62,
"FT%": 68,
"Assists": 4,
"Steals": 2.3,
"Blocks": 1.1,
"TRB": 13,
"Points Per Game": 12.5
},
{
"Season": 2012-2013,
"Team": "Club Providencia",
"Games": 48,
"FG%": 64.5,
"FT%": 70,
"Assists": 6,
"Steals": 3.8,
"Blocks": 2.1,
"TRB": 13,
"Points Per Game": 25
},
{
"Season": 2013,
"Team": "Academia de la Montana",
"Games": 17,
"FG%": 59.9,
"FT%": 62,
"Assists": 1.5,
"Steals": 1.2,
"Blocks": 1,
"TRB": 11.4,
"Points Per Game": 18.4
},
{
"Season": 2013-2014,
"Team": "Baskets Vilsbiburg",
"Games": 26,
"FG%": 59.3,
"FT%": 49.6,
"Assists": 1.5,
"Steals": 1.3,
"Blocks": 2,
"TRB": 14.8,
"Points Per Game": 24.7
},
{
"Season": 2015,
"Team": "Pirates de Bogota",
"Games": 20,
"FG%": 59.3,
"FT%": 54.8,
"Assists": 1.7,
"Steals": 2.3,
"Blocks": 0.7,
"TRB": 10.9,
"Points Per Game": 14.3
},
{
"Season": 2015-2016,
"Team": "CD Tinguiririca San Fernando",
"Games": 17,
"FG%": 56.8,
"FT%": 61.4,
"Assists": 1.1,
"Steals": 2.2,
"Blocks": 0.9,
"TRB": 11,
"Points Per Game": 13.1
},
{
"Season": 2016-2017,
"Team": "CD Universidad Catolica de Santiago",
"Games": 39,
"FG%": 48.1,
"FT%": 56.1,
"Assists": 1.8,
"Steals": 2.3,
"Blocks": 0.7,
"TRB": 11.6,
"Points Per Game": 15.2
}
{
"Career": " ",
"Team": " ",
"Games": 329,
"FG%": 66.8,
"FT%": 65.2,
"Assists": 3.5,
"Steals": 2.2,
"Blocks": 1.3,
"TRB": 13.1,
"Points Per Game": 17.4
}
]
Assuming you have a well-formatted JSON file, you can do It like this:
$(document).ready(function() {
// $.getJSON(url, callback);
$.getJSON('https://api.myjson.com/bins/aruak', function(json) {
tableGenerator('#tableName', json);
});
});
function tableGenerator(selector, data) { // data is an array
var keys = Object.keys(Object.assign({}, ...data));// Get the keys to make the header
// Add header
var head = '<thead><tr>';
keys.forEach(function(key) {
head += '<th>'+key+'</th>';
});
$(selector).append(head+'</tr></thead>');
// Add body
var body = '<tbody>';
data.forEach(function(obj) { // For each row
var row = '<tr>';
keys.forEach(function(key) { // For each column
row += '<td>';
if (obj.hasOwnProperty(key)) { // If the obj doesnt has a certain key, add a blank space.
row += obj[key];
}
row += '</td>';
});
body += row+'<tr>';
})
$(selector).append(body+'</tbody>');
}
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<table id="tableName"></table>