Related
This is the list that contains in the first key figure, all the attributes of my desired object, and in the later key figures, contains the value for each attribute in the line.:
[
[
"goalId",
"holderId",
"balance",
"taxes",
"netBalance",
"investmentIncome"
],
[
"1",
"1",
"33333333",
"150",
"150",
"1"
],
[
"5",
"5",
"1000",
"150",
"150",
"1"
],
[
"7",
"7",
"1000",
"150",
"150",
"1"
],
[
"11",
"12",
"1000",
"150",
"150",
"1"
],
[
""
]
]
And I want to turn the above list into a list of objects like this:
[
{
"goalId":1,
"holderId":1,
"balance":33333333,
"taxes":150,
"netBalance":150,
"investmentIncome":1
},
{
"goalId":5,
"holderId":5,
"balance":1000,
"taxes":150,
"netBalance":150,
"investmentIncome":1
},
{
"goalId":7,
"holderId":7,
"balance":1000,
"taxes":150,
"netBalance":150,
"investmentIncome":1
},
{
"goalId":11,
"holderId":12,
"balance":1000,
"taxes":150,
"netBalance":150,
"investmentIncome":1
}
]
I've already done it, but I think there's a better way to do it. The code was like this:
// Assuming that the variable already has the list loaded
const attributes = unformattedList[0];
unformattedList.splice(0, 1);
const arrayOfObjects = unformattedList.map(rawValue => {
const object = {};
rawValue.map((value, index) => {
object[attributes[index]] = value;
});
return object;
});
You shouldn't use .map() if your sole use-case for it is to iterate and cause side-effects - .forEach() is better for that task. However, you can still use .map() for this type of problem if you use it as intended (i.e use the new array it returns). First, you can get the keys of your objects by popping the first element from you array using .shift(). Then, you can .map() each inner array of values to an object (formed using Object.assign()), which contains all your values from the inner array:
const arr = [[ "goalId", "holderId", "balance", "taxes", "netBalance", "investmentIncome" ], [ "1", "1", "33333333", "150", "150", "1" ], [ "5", "5", "1000", "150", "150", "1" ], [ "7", "7", "1000", "150", "150", "1" ], [ "11", "12", "1000", "150", "150", "1" ], [ "" ]];
const keys = arr.shift();
const res = arr.filter(({length}) => length > 1).map(
vals => Object.assign({}, ...vals.map((v, i) => ({[keys[i]]: +v})))
)
console.log(res);
Above I am also using .filter(({length}) => length > 1)) to remove the uneeded [""] from your array. This will also remove empty arrays as well.
My question is about isolating an array element inside nested array and use it as the JSON field value with it's sibling array elements as a field value properties.
My Array:
"S,No.", "id", "date", "time"
eachArray = [
["1", "4", "2018/07/05", "22:27:35"],
["2", "1", "2018/07/05", "22:28:02"],
["3", "3", "2018/07/22", "21:25:51"],
["4", "3", "2018/07/22", "21:25:59"],
["5", "2", "2018/07/22", "21:25:59"],
["6", "1", "2018/07/25", "22:36:55"],
["7", "1", "2018/07/25", "21:25:51"],
]
I want to make a JSON object with the format like:
jsonArray = {
"2018/07/05" : ["4","1"],
"2018/07/22" : ["3","2"],
"2018/07/25" : ["1"]
}
My attempt so far:
eachArray = [
["1", "4", "2018/07/05", "22:27:35"],
["2", "1", "2018/07/05", "22:28:02"],
["3", "3", "2018/07/22", "21:25:51"],
["4", "3", "2018/07/22", "21:25:59"],
["5", "2", "2018/07/22", "21:25:59"],
["6", "1", "2018/07/25", "22:36:55"],
["7", "1", "2018/07/25", "21:25:51"],
]
var outerSet = [];
var jsonArray = {};
eachArray.forEach((val, index) =>{
var outerLast = outerSet.pop();
var set = eachArray[index][1];
var dateHere = eachArray[index][3];
outerSet.push(set);
if(index !== 0){
if(outerLast !== dateHere){
outerSet = [];
jsonArray[dateHere] = set;
return;
}
}
outerSet.push(dateHere);
jsonArray[dateHere] = outerSet;
});
Try reduce-ing into an object indexed by date, whose values are Sets (to deduplicate), to which you add the number in each item, and then once you've gone through the eachArray, convert the Sets back to arrays:
const eachArray = [
["1", "4", "2018/07/05", "22:27:35"],
["2", "1", "2018/07/05", "22:28:02"],
["3", "3", "2018/07/22", "21:25:51"],
["4", "3", "2018/07/22", "21:25:59"],
["5", "2", "2018/07/22", "21:25:59"],
["6", "1", "2018/07/25", "22:36:55"],
["7", "1", "2018/07/25", "21:25:51"],
]
const itemsByDate = eachArray.reduce((a, [, num, date]) => {
if (!a[date]) a[date] = new Set();
a[date].add(num);
return a;
}, {});
Object.entries(itemsByDate).forEach(([key, set]) => {
itemsByDate[key] = [...set];
});
console.log(itemsByDate);
You can also use a plain object to store date as key and values as a Set. Values should be stored on a set so that it won't have duplicates. Then at the end, convert all the Sets to Arrays using Array.from function.
eachArray = [
["1", "4", "2018/07/05", "22:27:35"],
["2", "1", "2018/07/05", "22:28:02"],
["3", "3", "2018/07/22", "21:25:51"],
["4", "3", "2018/07/22", "21:25:59"],
["5", "2", "2018/07/22", "21:25:59"],
["6", "1", "2018/07/25", "22:36:55"],
["7", "1", "2018/07/25", "21:25:51"],
];
var obj = {};
eachArray.forEach(arr => {
if(!obj[arr[2]]){
obj[arr[2]] = new Set();
}
obj[arr[2]].add(arr[1]);
});
Object.keys(obj).forEach(key => obj[key] = Array.from(obj[key]));
console.log(obj);
const input = [
["1", "4", "2018/07/05", "22:27:35"],
["2", "1", "2018/07/05", "22:28:02"],
["3", "3", "2018/07/22", "21:25:51"],
["4", "3", "2018/07/22", "21:25:59"],
["5", "2", "2018/07/22", "21:25:59"],
["6", "1", "2018/07/25", "22:36:55"],
["7", "1", "2018/07/25", "21:25:51"],
];
const output = input.reduce((accu, [, num2, date,]) => {
if(!accu[date]) accu[date] = [];
if(!accu[date].includes(num2)) accu[date].push(num2);
return accu;
}, {});
console.log(output);
I have an array of strings and I want to make a new one joining the strings of the arrays until the delimeter character is shown.For example I have this array of strings:
["5", "2", "0", "1", "6", "2", "7", "0", "1", "9", "9", "0", "0", "", "5", "2", "0", "3", "4", "1", "1", "0", "2", "1", "9", "8", "4", ""]
and I want to make a new one like this:
["5201627019900","5203411021984"]
The delimeter of those items in the array is the "" between them.Any help?
var result = array.join("").trim().split(/\s/ /*or " " */);
var result = array.reduce((a,b) => { return a+b; }).trim().split(" ");
Script:
vL1 = ["AB", "AB", "AB", "AB", "AB", "CS", "CS", "CS", "ND", "ND"];
vL2 = ["1", "1", "1", "2", "3", "1", "1", "2", "1", "1"];
for(var i = 0; i < vL1.length; i++){
thing = vL1[i] + " " + vL2[i];
console.log(thing);
}
When I check the developer console, I see the following:
(3) AB 1
AB 2
AB 3
(2) CS 1
CS 2
(2) ND 1
How can I modify the script so I can get the number of times AB with 1 appeared or CS with 1 appeared in my code to be used in other functions?
I just want to know the count for each vL2 that is represented in vL1. It is important to associate vL1 because that will let me identify the vL2, since it is not unique.
You might also do as follows;
var vL1 = ["AB", "AB", "AB", "AB", "AB", "CS", "CS", "CS", "ND", "ND"],
vL2 = ["1", "1", "1", "2", "3", "1", "1", "2", "1", "1"],
result = vL1.reduce((p,c,i) => p[c] ? (p[c][vL2[i]] = p[c][vL2[i]] ? ++p[c][vL2[i]]
: 1, p)
: (p[c] = {[vL2[i]]: 1}, p), {});
console.log(result);
You can store the counts in an object. Also, utilizing Array.prototype.reduce can make it simpler to work with the indexes (e.g. you don't have to handle incrementing the index manually, etc.):
vL1 = ["AB", "AB", "AB", "AB", "AB", "CS", "CS", "CS", "ND", "ND"];
vL2 = ["1", "1", "1", "2", "3", "1", "1", "2", "1", "1"];
var counts = vL1.reduce(function(counts,vL1Element,index) {
//initialize this index if it isn't set
if(counts[vL1Element] == undefined) {
counts[vL1Element] = {};
}
//set this count to 0 if it hasn't been set yet
if (counts[vL1Element][vL2[index]] == undefined) {
counts[vL1Element][vL2[index]] = 0;
}
counts[vL1Element][vL2[index]]++;
return counts;
},{});
console.log(counts);
var obj={};
function log(a){
if(obj[a]){
obj[a]++;
}else{
obj[a]=0;
}
}
And then do:
log(thing);
Inside of your for loop, and than:
console.log(obj);
Obj now contains:
AB1:3;
....
How can I append a text to only the first value of an array and send the array back to the server. This is the response i am getting from the server
[{
"12": [ "12", "1", "2", "3" ]
}, {
"13": [ "13", "1", "2", "3" ]
}, {
"14": [ "14", "1", "2", "3" ]
}, {
"15": [ "15", "1", "2", "3" ]
}]
I want my array to now be like this so that i can pass this array to my data table.
var object = [{
12: ["12 hrs", "1", "2", "3"]
}, {
13: ["13 hrs", "1", "2", "3"]
}, {
14: ["14 hrs", "1", "2", "3"]
}, {
15: ["15 hrs", "1", "2", "3"]
}]
How can I append hrs to the first value?
Here's one easy method...
// Parse the string response from the server into an object
var obj = JSON.parse(serverResponse);
// loop through the object, knowing that it's nested in the first index of an array "obj[0]"
for(id in obj[0]){
// obj->arrayIndex->objectProperty->arrayIndex append " hrs"
obj[0][id][0] += " hrs";
}
// to return this to the server, apply JSON.stringify(obj)
I fixed object in your question to -
var objectArray = [{"12" : ["12", "1", "2", "3"]}, {"13" : ["13", "1", "2", "3"]},{"14" : ["14", "1", "2", "3"]},{"15" : ["15", "1", "2", "3"]}]
This is how you can loop through the objectArray and change in the way you wanted -
for(var i=0;i<objectArray.length;i++)
{
for(var key in objectArray[i])
{
objectArray[i][key][0]+=" hrs";
break;
}
}
console.log(objectArray)