I have a knockout view model which contains an array of Row objects and an array of VisibleColumns objects as shown in the following screen grab:
As you can see, the VisibleCoumns array contains values which match the keys of the Rows array.
I would like to remove key value pairs of the Rows array where the key cannot be found in the VisibleColumns array.
These array are going to be quite large so I'm wondering if there's a typical javascript way of doing this kind of thing that is quite efficient?
Something like this:
for (var i = 0; i < Rows.length; i++) {
var row = Rows[i];
var keys = Object.keys(row);
for (var k = 0; k < keys.length; k++) {
if (VisibleColumns.indexOf(keys[k]) === (-1)) {
delete row[keys[k]];
}
}
}
Related
I have a nested array structure that looks like this below. Lets call it arr:
The innermost array has a key and value pair. But as it can be seen, the values are only from 3-7. I need to fill this innermost array with values 0-9 in the key column and populate them to 0 if the key doesn't already exist. I tried creating a separate array(target) with values [0,9] to check against but have been unsuccessful. I tried to do something like this but I am getting an error:
var target = [0,1,2,3,4,5,6,7,8,9];
for(let i =0; i < target.length; i++){
for(let j =0; j < arr.length; j++){
for(let k =0; k < arr[j].values.length; k++){
if (miss_rate_arr[i] in rating_count[j].values[k].key === false){
rating_count[j].values.push({key:miss_rate_arr[i], value:0});
}
}
}
}
console.log(rating_count);
This is not only checking that particular value at that 0 location and I understand the problem. I am just not sure how to fix it. I am expecting the end result to look something like this below. Thank you in advance
I'm not exactly sure what youre going for,
If you're going for a more general approach and want to keep the target array,
this is more like what you posted, and you'd need to sort it afterwards to get the array in the order you posted..
let target = [0,1,2,3,4,5,6,7,8,9];
for(let i = 0; i < target.length; i++){
if (arr.filter(x => x.key == target[i]).length == 0){
arr.push({key:target[i], value:0});
}
}
arr.sort((a,b) => a.key - b.key);
But if target is always 0-9, you could skip the target array and just loop from 0 to the max key you're aiming for.
that way the test for the value is more simple (you can just check if the key matches the index) and since you have to add it at the correct index, you keep it sorted..
let maxKey = 9;
for(let i = 0; i <= maxKey; i++){
if (arr.length > i && arr[i].key != i || arr.length == i){
arr.splice(i, 0, {key: i, value:0});
}
}
I have an array of names. I also have an array of objects. I would like to iterate through the array of objects and also iterate through the the array of names and add the name into the objects. For example, name[0] goes into object[0], and so on.
I have this code:
this.individualSrv.GetDataById(this.org[i].userId).subscribe(data => {
this.names.push(data.fullname)
for (var x = 0; x < this.org.length; x++) {
for (var i in this.names) {
this.org[x]['name'] = this.names[i]
}
}
})
Right now, the last name in the array is added to each object in the array.
You don't need to nest 2 loops to do that. Just make sure that both arrays have the same length.
this.individualSrv.GetDataById(this.org[i].userId).subscribe(data => {
this.names.push(data.fullname)
for (var x = 0; x < this.org.length; x++) {
this.org[x]['name'] = this.names[x]
}
})
I have an array of data as follows:
var Sonuc = [[{"ID":8,"Number":"1","Name":"Ahmet"}],
[{"ID":7,"Number":"2","Name":"Semih"}],
[{"ID":6,"Number":"3","Name":"Derviş"}],
[{"ID":8,"Number":"4","Name":"Derviş"},{"ID":9,"Number":"4","Name":"Veli"}],
[{"ID":11,"Number":"44","Name":"Zeki"},{"ID":45,"Number":"44","Name":"Veli"}]]
I tried to write datas to console for each object as follows, but it does not work:
for (var i = 0; i < 3; i++) {
for(var obj in Sonuc[i]) {
console.log(obj.Number);
};
}
How can I output the Number value for each data on console?
The problem is that you have an array or arrays, with the sub-arrays each containing one or more objects.
Your problem is you are not specifying the index for the sub-arrays. You can access the first object like this:
console.log(obj[0].Number);
That will get you some output at at least, but it is confusing exactly what data you want to get. That 3 loop makes no sense...
If you want to output all objects, then you should first loop the sub-arrays, and then loop the objects. Something like this:
var Sonuc = [[{"ID":8,"Number":"1","Name":"Ahmet"}],
[{"ID":7,"Number":"2","Name":"Semih"}],
[{"ID":6,"Number":"3","Name":"Derviş"}],
[{"ID":8,"Number":"4","Name":"Derviş"},{"ID":9,"Number":"4","Name":"Veli"}],
[{"ID":11,"Number":"44","Name":"Zeki"},{"ID":45,"Number":"44","Name":"Veli"}]];
for (var i = 0; i < Sonuc.length; i++) {
var arr = Sonuc[i];
for (var j = 0; j < arr.length; j++) {
var obj = arr[j];
console.log(obj.Number);
}
}
I am trying to iterate through an object which has unknown number of arrays and also i dont know number of data in each array because im pulling it from a database like this for example.
i can know how many arrays in object with data.length so first for loops condition will be data.length but because i dont know how many data in each array how can i decide second for loop condition ? thanks
for(var i = 0; i <= data.length; i++) {
for(var k = 0; k <= ????; k++) {
data[i][k].locationlat
}
}
So you want to iterate through a 2-dimensional array? Would something like this work?
var num_d1 = data.length;
for (var i=0; i<num_d1; i++) {
var num_d2 = data[i].length;
for (var k=0; k<num_d2; k++) {
console.log('Latitude: ', data[i][k].locationlat)
}
}
I’m looking to use arrays to create unique names for variables. I am not looking to store any calculated values in the arrays, but rather be able to declare variables using arrays to store the values. My attempts and research how to accomplish this leads me to think that it’s not even possible. I’d appreciate it if someone could let me know and if it is possible an answer/example on how to do it. I’ll post a simplified example on what I’m hoping to get working.
var indices = ["index01", "index02", "index03"];
var keys = ["key01", "key02", "key03"];
for (var index = 0; index < indices.length; index++)
{
for (var key = 0; key < keys.length; key++)
{
var indices[index]+keys[key] //Looking for var index01key01, var index01key02 etc...
}
}
Well, basic javascript variables are in the window scope, so try:
var indices = ["index01", "index02", "index03"];
var keys = ["key01", "key02", "key03"];
for (var index = 0; index < indices.length; index++)
{
for (var key = 0; key < keys.length; key++)
{
// you can now use the variable as window.index01key01, or just index01key01
window[indices[index]+keys[key]] = null;
}
}