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;
....
Related
Given Header array and make all other arrays into same format of header array:
HEADER ["", "", "a", "b", "", "c", "d","",""]
1. ["", "", "1", "2", "", "3", "4"] ==== CORRECT FORMAT
2. ["1","2","", "3", "4","",""] ==== WRONG FORMAT
3. ["", "", "", "", "1", "2", "", "3", "4"] = WRONG FORMAT
Output array :
1. ["", "", "1", "2", "", "3", "4"]
2. ["", "", "1", "2", "", "3", "4"]
3. ["", "", "1", "2", "", "3", "4"]
Header and input array length is same. But the output array length must be till the last of element of the header array.
I am unable to fix all types of inputs with below algorithm:
alignArray(arr1, arr2) {
let arr3 = [];
let arr4 = _.compact(arr2);
let count = 0;
_.map(arr1, function (num, index) {
if (_.isString(num) && !(_.isEmpty(num))) {
if (arr4[count])
arr3.push(arr4[count]);
else
arr3.push("");
count++;
} else {
arr3.push("");
}
});
return arr3;
}
One approach is to treat the first input array like a queue, dequeueing items whenever b has a value. Based on your update, I've trimmed off empty strings at the end of the output array.
const alignArray = (a, b) => {
a = a.filter(e => e);
b = b.map(e => e === "" ? e : a.shift() || "");
let idx = b.length - 1;
for (; idx >= 0 && b[idx] === ""; idx--);
return b.slice(0, idx + 1);
};
const header = ["", "", "a", "b", "", "c", "d", "", ""];
const rows = [
["", "", "1", "2", "", "3", "4", "", ""],
["1","2","", "3", "4","",""],
["", "", "", "", "1", "2", "", "3", "4"]
];
rows.forEach(e => console.log(alignArray(e, header)));
Hi guys can someone please help me with some code, so i have two arrays a and b i want to divide a by b. Here is my arrays var a = [
"14",
"8",
"3",
"5"
];
Var b = [
"44",
"16",
"30",
"11"
];
So i want my result to be an array. Please can someone help me
You can use Array.map for this
var a = [ "14", "8", "3", "5" ];
var b = [ "44", "16", "30", "11" ];
let res = a.map((d, i) => d/b[i])
console.log(res)
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);
Following JSON : https://api.myjson.com/bins/3yz34
I was thinking I might use an internal ID for a key (which simplifies looping through several entities and setting up data gathering) and use a value for display. However, I cannot get the value of the key.
$.each(tabledata.Companies, function (){
company = this;
alert(company);
var test = Object.keys( company );
console.log(test);
});
Object.keys() just gives me the number of characters in the value.
var test = Object.keys( company );
Returns:
["0", "1", "2", "3", "4", "5", "6", "7"]
["0", "1", "2", "3", "4", "5", "6", "7", "8"]
["0", "1", "2", "3", "4", "5", "6"]
["0", "1", "2", "3", "4"]
["0", "1", "2", "3", "4", "5", "6"]
Which seems to be the number of characters from the value in the key-value pair? Pretty stuck on this one, any ideas? Or maybe setup the JSON differently?
Thanks in advance!
Change the signature of your loop function to pass in the parameters
$.each(tabledata.Companies, function(key, value) {...}
See demo below
DEMO UPDATED based on comments
var tabledata = {
"Companies": {
"8df135f4-db42-486c-8d8c-fbbdd561c25e": "Phillips",
"47d946e3-56db-4bc7-8472-3809eb48506d": "Bauknecht",
"3ae13b97-7a09-4342-8953-c1d5a55687db": "Capitol",
"fb017214-dbc1-4b71-8080-4f9b530f4b49": "Bosch",
"50ede412-4eb0-429b-8d73-a1bfef41ebbc": "LG Corp"
},
"Pets": {
"dog": "Snoopy",
"cat": "Silvester",
"bird": "Sweetie",
"snake": "Snaker",
"fish": "Goldie Locks"
}
}
var attributeName="Companies";
$(function() {
$.each(tabledata[attributeName], function(key, value) {
console.log("KEY:" + key + " -> Value:" + value);
});
attributeName="Pets";
$.each(tabledata[attributeName], function(key, value) {
console.log("KEY:" + key + " -> Value:" + value);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
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)