I'm having some slight trouble with this code. The problem for it seemed easy enough; to push the keys into my array and then push the values into my array separately. Yet, when I have the code returned to me, it shows that nothing is pushed into my array, and it tells me the function parameter "obj" isn't iterable. I'm not sure what's wrong.
function giveMeFive(obj){
//coding here
var arr=[];
for(var key in obj){
if( (key.length==5)){
arr.push(obj[key]);
}
}
for(var value of obj){
if(value.length==5){
arr.push(obj[value]);
}
}
return arr;
}
Related
I'm using a for x in loop to check if a value is == to [] and if so remove the property using remove but it just does not seem to work.
const whosOnline = (a) => {
var obj = { online:[],
offline:[],
away:[] };
for(let i = 0; i < a.length; i++){
if(a[i].lastActivity > 10 && a[i].status == 'online'){obj.away.push(a[i].username)}
else if(a[i].status == 'offline'){obj.offline.push(a[i].username)}
else{obj.online.push(a[i].username)}
}
for(let x in obj){
console.log(obj[x])
if(obj[x] === []){delete obj[x]}}
return obj
}
you were close, however you also need to reference the array index for each object key value. Comments explaining this in the code below.
var obj = { online:[],
offline:[],
away:[] };
for(var x in obj){
if(!obj[x][0]){ // The 0 is the index inside the online array, next loop it will be the offline array and then the away array.
console.log('The array is empty');
// Do what you want to do now that it is empty
// This will continue to loop through and check the value of all the keys in the object.
}
}
console.log('done');
Good luck -
Mitch from
https://spangle.com.au
Using some debugging (simply testing if a console.log gets printed for instance) you find that your if-condition is never true.
This is because you test if an array equals a newly created empty array. This can never be the case, because objects are compared by object reference instead of value.
Instead you want to probably test to see if your array is empty by doing ‘if(obj[x].length===0)’ (or shorter: ‘if(!obj[x].length)’)
I have following object im not sure how to proceed.
Object image
How can I go through all objects and select the content array and search for a value x. And when the value x is in the object I need to get the object title from the object where the value was found.
Can anyone give me a hint how I can solve this problem?
you can use for...in to iterate over the object and indexOf() to check if a key exists in the array content. something like this:
function searchVal(x){
for(var key in obj){
if(obj[key].hasOwnProperty('content') && obj[key].content.includes(x))
return key;
}
}
You can use for...in to iterate the object keys, then a regular for loop to check the content array for your specific value:
function findTitle(x) {
for (var key in obj) {
for (var i = 0; i < obj[key].content.length; i++) {
if (obj[key].content[i] === x) {
return key;
}
}
}
}
let name = Object.values( obj /*your main object*/ )
.find( obj => obj.content.includes(x) )
.name;
You could find the first object in the Objects values of your main obj, that has a property content which includes x, then get the name of that object.
I have a function which takes a list of dictionaries [{}] as an argument. It manipulates this list of dicts by adding a new key: value pair to it where value is again a list of dictionaries. This is what the function looks like, I've added comments to explain it.
function addFilesToProjects(nonUniqueArray, lists) {
var fileList = [{}]; //this will contain the list of dictionaries that I want to add as a key to the array 'nonUniqueArray'
var filesArray = []; //this was just for testing purposes because I want to access the modified version of nonUniqueArray outside the function, which I'm not able to (it shows undefined for the new key:value pair)
for (var i = 0; i < nonUniqueArray.length; i++) {
lists.forEach(function (list) {
fileNameString = JSON.stringify(list['name']).slice(2, -2);
if (fileNameString.indexOf(nonUniqueArray[i]['title']) !== -1 && fileNameString !== nonUniqueArray[i]['title']) {
fileList.push({
'name': fileNameString
});
}
});
nonUniqueArray[i]['files'] = fileList;
//this logs out the right key:value pair to the console
console.log(nonUniqueArray[i]);
filesArray.push(nonUniqueArray[i]);
while (fileList.length > 0) {
fileList.pop();
}
}
//however, now I get everything as before except the new 'files' key has empty list [] as its value :(
console.log(nonUniqueArray);
return filesArray;
}
I have no clue why is this happening, can someone help?
You seem to think that you are adding a copy of fileList into each dictionary, but in fact are adding the same fileList into each (that is, each is a reference to the same object) so that, as #vlaz points out, when you empty out the original, you are in fact emptying out what appears in each dictionary.
i have an object evt.participators that contains objects. when loggin it , i show this result:
I want to read all the id of evt.participators objects. I try this code :
var t=0 ;
angular.forEach(evt.participators, function(value, key){
console.log(evt.participators.$id );
t++;
});
But i get three time undefined.
How can i fix it please ?
You can easily retrieve all the keys in the object by using a for...in loop
for (var key in evt.participators) {
if (evt.participators.hasOwnProperty(key)) {
console.log(key);
}
}
JsFiddle: https://jsfiddle.net/er647anm/1/
use for..in for loop through object
for (var prop in evt.participators) {
console.log(evt.participators[prop]);
}
for more information please follow For..In Documentation
What's the correct format for an object containing an array of comma separated numbers?
Here is what I am doing, but I am unsure if it is the correct way:
var myObj = {
'coord1' : { 'section-a' : [216,259,216,294,165,294,165,259,216,259] },
'coord2' : { 'section-a' : [20,218,8,178,3,143,6,112,13] }
};
I'd like to access the coordinates of a section by the following:
for(var coord in myObj){
for(var section in coord){
alert(section);
}
}
And have the raw coordinates returned as a comma separated string. Any suggestions?
Unfortunately for in loops don't do quite what you're expecting.
The variable that you create is property name of the current iteraton when you iterate over an object. It is the index of the array when you iterate over an array.
for(var prop in myObj){
for(var i in myObj[prop]){
alert(myObj[prop][i]);
}
}
Note, when iterating over objects you may only want to iterate on direct properties of that object, not properties up the protype chain. Use hasOwnPrototype if you only want to iterate over direct properties on the object.
for(var prop in myObj){
if(myObj.hasOwnProperty(prop)) {
for(var i in myObj[prop]){
if(myObj[prop].hasOwnProeprty(i)) {
alert(myObj[prop][i]);
}
}
}
}
Remember that a for in loop in javascript returns an objects property key not the object the key refers to. Also use .hasOwnProperty to avoid possibly looping over an objects prototype.
You would need to change it to:
for (var coord in myObj){
if(myObj.hasOwnProperty[coord]){
var coord = myObj[coord];
for(var section in coord)
if(coord.hasOwnProperty[section]){
alert(coord[section]);
}
}
}
}