Suppose I have a code like this:
function example() {
const obj = {};
for (let i = 0; i < 10; i++) {
for (let j = 0; j< 10; j++) {
if (obj[i] == undefined) {
obj[i] = 0;
}
if (obj[j] == undefined) {
obj[j] = 0;
} else {
obj[i] += i;
obj[j] += j;
}
}
}
}
Here you can see:
if (obj[i] == undefined) {
obj[i] = 0;
}
I check if i not in obj I assign the key i inside obj with 0 else I do nothing, I do sample with j.
The code is duplicated and I do not want to repeat my self, it is bad practice so I make another function to apply for i and j like this:
function initObjWithValue(obj, keys) {
for (const key of keys) {
if (obj[key] === undefined) {
obj[key] = 0;
}
}
}
The function is very easy to understand right? It has first parameter as an object and the second parameter is an array of keys to check. Now I can refactor my code like this:
function example() {
const obj = {};
for (let i = 0; i < 10; i++) {
for (let j = 0; j< 10; j++) {
initObjWithValue(obj, [i, j]);
obj[i] += i;
obj[j] += j;
}
}
}
The code is clearer but as you can see in my function initObjWithValue, it mutates obj and I think it is not good. Here is the quote from Wiki page:
In computer programming, a pure function is a function that has the following properties:
Its return value is the same for the same arguments (no variation with local static variables, non-local variables, mutable reference arguments or input streams from I/O devices).
And I got stuck at this point, how can I do not repeat my self and I can achieve pure function in this case?
You could instead make initObjectWithValue return a new object, which you can then merge with your current obj. This way, all you're doing is reading from obj, and not mutating it within initObjectWithValue:
function initObjWithValue(obj, keys) {
const tmp = {};
for (const key of keys) {
if (!(key in obj)) {
tmp[key] = 0;
}
}
return tmp;
}
function example() {
const obj = {};
for (let i = 0; i < 10; i++) {
for (let j = 0; j< 10; j++) {
Object.assign(obj, initObjWithValue(obj, [i, j])); // merge the return with current object
obj[i] += i;
obj[j] += j;
}
}
return obj;
}
console.log(example());
Instead of checking
(obj[i] == undefined)
write
(typeof obj[i] == undefined)
Related
Iam not able to reference a variable from outside the loop in the following function. I want to use the value inside the function itself but the scope doesnt seem to allow it.
function coinOnTheTable(m, k, board) {
for (var i = 0; i < board.length; i++) {
for (var j = 0; j < m; j++) {
if (board[i][j] === "*") {
const a = `${i}${j}`;
return a;
}
}
}
}
Works, While
function coinOnTheTable(m, k, board) {
for (var i = 0; i < board.length; i++) {
for (var j = 0; j < m; j++) {
if (board[i][j] === "*") {
const a = `${i}${j}`;
}
}
}
return a;
}
gives an error
return a;
^
ReferenceError: a is not defined
That's because a variable defined as const or let has "block scope". That means it is only visible inside the nearest set of curly braces. So:
function foo() {
const foo = 'foo-string';
if(1) {
const bar = 'bar-string';
console.log(bar); // This is fine
}
console.log(foo); // This is also fine
console.log(bar); // Error!
}
To solve your problem, you need to define your variable outside of the block defined by the nested for loop. So:
function coinOnTheTable(m, k, board) {
let a;
for(var i = 0; i < board.length; i++){
for(var j = 0; j < m; j++){
if(board[i][j] === "*"){
a = `${i}${j}`;
}
}
}
return a;
}
Note, the reason it changes from const to let is because const variables cannot be reassigned, but since you want to declare it as undefined (at the start of the function, then assign a string to it (inside the loop), you need to be able to reassign it.
make a function scope:
function coinOnTheTable(m, k, board) {
let a;
for(var i=0;i<board.length;i++){
for(var j=0;j<m;j++){
if(board[i][j]==="*"){
const a=`${i}${j}`;
}
}
}
return a;
}
In the following code there is a console log of obj['mn'] which returns the length of that specific object which is 2. The problem with the code is that it doesn't count the multidimentional array, and only it counts the first array. The result should be 4 because there are 4 'mn' in total. What am I doing wrong?
var arr = [['ab','pq','mn','ab','mn','ab'],'mn','mn'];
var obj = { };
for (var i = 0, j = arr.length; i < j; i++) {
if (obj[arr[i]]) {
obj[arr[i]]++;
}
}
console.log(obj['mn']);
This is what you're looking for:
var arr = [['ab','pq','mn','ab','mn','ab'],'mn','mn'];
var obj = { };
function count(arr, obj) {
for (var i = 0, j = arr.length; i < j; i++) {
if (Array.isArray(arr[i])) {
count(arr[i], obj);
}
else if (typeof obj[arr[i]] !== 'undefined') {
obj[arr[i]]++;
}
else {
obj[arr[i]] = 1;
}
}
return obj;
}
console.log(count(arr, obj));
This is a recursive implementation. When it gets to an array, the recursion get one level deeper.
You are calling obj[['ab','pq','mn','ab','mn','ab']], which is obviously not what you wanted.
You need a depth first search.
If arr[i] is an array, then you need to loop through that array.
I'd like to use an object to configure some settings for an app. My idea is to start with this:
var obj = {
property_one: 3;
property_two: 2;
property_three: 1;
}
And I would like to end up with this:
var array = [
'property_one','property_one','property_one',
'property_two','property_two',
'property_three'
]
My current solution is to do this for each property:
function theConstructor(){
for(i=1; i <= obj.property_one; i++){
this.array.push('property_one');
};
for(i=1; i <= obj.property_two; i++){
this.array.push('property_two');
};
for(i=1; i <= obj.property_two; i++){
this.array.push('property_two');
};
}
But this gets tedious, because I might have many properties, and these might change as the app evolves.
I know I can loop through object's properties like this:
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
array.push(key);
}
}
But this will push the value to the array, not the key (as a string). Any ideas about how I can do this more efficiently?
Try this
function theConstructor(){
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
for(var i=1; i <= obj[key]; i++){
this.array.push(key);
};
}
}
}
Using Array.prototype.reduce():
var obj = {
property_one: 3,
property_two: 2,
property_three: 1
};
var resultArray = Object.keys(obj).reduce(function(result, curItem) {
for (var index = 0; index < obj[curItem]; index++) {
result.push(curItem);
}
return result;
}, []);
document.write(JSON.stringify(resultArray));
var each = _.each = _.forEach = function(obj, iterator, context) {
if (obj == null) return obj;
if (nativeForEach && obj.forEach === nativeForEach) {
obj.forEach(iterator, context);
} else if (obj.length === +obj.length) {
for (var i = 0, length = obj.length; i < length; i++) {
if (iterator.call(context, obj[i], i, obj) === breaker) return;
}
} else {
var keys = _.keys(obj);
for (var i = 0, length = keys.length; i < length; i++) {
if (iterator.call(context, obj[keys[i]], keys[i], obj) === breaker) return;
}
}
return obj;
};
In the implementation of _.each method in underscore.js, the loop variables are set as follows.
for (var i = 0, length = obj.length; i < length; i++)
why is the length variable explicitly declared?, isn't the following code more succinct?
for (var i = 0; i < obj.length; i++)
There are a couple possible reasons to declare the separate length variable:
Caching it locally in its own variable can be faster than referencing it as a property every time through the loop.
In some cases, the array length may grow during the iteration and you may not want to iterate newly added elements.
I want to print a key: value pair from javascript object. I can have different keys in my array so cannot hardcode it to object[0].key1
var filters = [{"user":"abc"},{"application":"xyz"}];
console.log(Object.keys(filters[0])[0]); // prints user
var term = (Object.keys(filters[0])[0]);
console.log(filters[0].term); // prints undefined
How can i print the value of the key
for (var key in filters[0]){
console.log( key + ": " + filters[0][key]);
}
Or if you want to print all the values of filters
for (var i in filters){
console.log(i);
for (var key in filters[i]){
console.log( key + ": " + filters[i][key]);
}
}
##On #mplungjan 's comment
filters.forEach(function(obj, index){
console.log(index);
for (var key in obj){
console.log(key, obj[key]);
}
});
This is looking for a term property on filters[0]:
console.log(filters[0].term);
What you actually want to do is use the value of term (in your example that will be "user") as the property identifier:
console.log(filters[0][term]);
for loop for array and for..in iteration for object:
var filters = [{ "user": "abc"}, {"application": "xyz"}];
for (var i = 0; i < filters.length; i++) { // the plainest of array loops
var obj = filters[i];
// for..in object iteration will set the key for each pair
// and the value is in obj[key]
for (var key in obj) {
console.log(key, obj[key])
}
}
ES6
[{ "user": "abc"}, {"application": "xyz"}].forEach(
obj => console.log(Object.entries(obj).flat())
)
You can access the value using array syntax
var filters = [{"user":"abc"},{"application":"xyz"}];
console.log(Object.keys(filters[0])[0]); // prints user
var term = (Object.keys(filters[0])[0]);
console.log(filters[0][term]);// prints abc
Lets say that we have a mode object that has some strings in it for example. If we were to do MODE.toString() with just alpha, beta, gamma in the object, what will be returned is [object Object] which is not useful.
Instead, lets say we wanted to get something nice back like Normal, Sepia, Psychedelic. To do that, we could add a toString: function(){...} to our object that will do just that. One catch to this however is that if we loop through everything in the object, the function it self will also be printed, so we need to check for that. In the example I'll check toString specifically, however, other checks like ... && typeof MODE[key] == "string" could be used instead
Following is some example code, calling MODE.toString(); will return Normal, Sepia, Psychedelic
var MODE = {alpha:"Normal", beta:"Sepia", gamma:"Psychedelic",
toString: function() {
var r = "";
for (var key in MODE) {
if (MODE.hasOwnProperty(key) && key != "toString") {
r+= r !== "" ? ", ":"";
r+= MODE[key];
}
}
return r;
}
};
if you want get all keys in array of object, you can try this one mybe
let temp = []
let keys = []
let result = []
for (let i = 0; i < data.length; i++) {
temp = Object.keys(data[i])
for (let j = 0; j < temp.length; j++) {
if(!keys.includes(temp[j])){
keys.push(temp[j])
}
}
temp = []
}
for (let i = 0; i < data.length; i++) {
for (let j = 0; j < keys.length; j++) {
if(data[i][keys[j]] == undefined){
data[i][keys[j]] = ""
}
}
}
return data
or this one if you want take the key from same array 2dimension
function convertObj(arr){
let arrResult = []
for (let i = 1; i < arr.length; i++) {
let obj={}
for (let j = 0; j < arr[0].length; j++) {
obj[arr[0][j]] = arr[i][j]
}
arrResult.push(obj)
}
return arrResult
}
If you want to print key and value, even for nested object then you can try this function:
function printObjectData(obj, n = 0){
let i = 0;
var properties = Object.keys(obj);
let tab = "";
for(i = 0; i < n; i++)
tab += "\t";
for(i = 0; i < properties.length; i++){
if(typeof(obj[properties[i]]) == "object"){
console.log(tab + properties[i] + ":");
printObjectData(obj[properties[i]], n + 1);
}
else
console.log(tab + properties[i] + " : " + obj[properties[i]]);
}
}
printObjectData(filters);
and the solution will look like this:
0:
user : abc
1:
application : xyz
and if you want to remove 0: and 1:
then simply remove
console.log(tab + properties[i] + ":");
after the if statement