Signup/Sign In

Answers

All questions must be answered. Here are the Answers given by this user in the Forum.

With the following extension method you can get away without reflection:

***public static Type GetListType(this List _)
{
return typeof(T);
}***

Or more general:

***public static Type GetEnumeratedType(this IEnumerable _)
{
return typeof(T);
}***

Usage:

***List list = new List { "a", "b", "c" };
IEnumerable strings = list;
IEnumerable objects = list;

Type listType = list.GetListType(); // string
Type stringsType = strings.GetEnumeratedType(); // string
Type objectsType = objects.GetEnumeratedType(); // BEWARE: object***
3 years ago
To get only one value in a multi value **INSERT** from another table I did the following in SQLite3:

***INSERT INTO column_1 ( val_1, val_from_other_table )
VALUES('val_1', (SELECT val_2 FROM table_2 WHERE val_2 = something))***
3 years ago
Use a **case** statement:

***select id,
case report.type
when 'P' then amount
when 'N' then -amount
end as amount
from
`report`***
3 years ago
You need to decode the bytes object to produce a string:

***>>> b"abcde"
b'abcde'

# utf-8 is used here because it is a very common encoding, but you
# need to use the encoding your data is actually in.
>>> b"abcde".decode("utf-8")
'abcde'***
3 years ago
**list(newdict)** works in both Python 2 and Python 3, providing a simple list of the keys in **newdict. keys()** isn't necessary.
3 years ago
A simple and effective one-liner:

***function isMobile() { return ('ontouchstart' in document.documentElement); }***

However, above code doesn't take into account the case for laptops with touchscreen.

***function isMobile() {
try{ document.createEvent("TouchEvent"); return true; }
catch(e){ return false; }
}***
3 years ago
Ecma script 5 (ES5) which all browser support and precompiled. ES6/ES2015 and ES/2016 came this year with lots of changes so to pop up these changes there is something in between which should take cares about so TypeScript.

• TypeScript is Types -> Means we have to define datatype of each property and methods. If you know C# then Typescript is easy to understand.

• Big advantage of TypeScript is we identify Type related issues early before going to production. This allows unit tests to fail if there is any type mismatch.
3 years ago
You can add two properties to **Element.prototype** to get the top/left of any element.

***Object.defineProperty( Element.prototype, 'documentOffsetTop', {
get: function () {
return this.offsetTop + ( this.offsetParent ? this.offsetParent.documentOffsetTop : 0 );
}
} );

Object.defineProperty( Element.prototype, 'documentOffsetLeft', {
get: function () {
return this.offsetLeft + ( this.offsetParent ? this.offsetParent.documentOffsetLeft : 0 );
}
} );***

This is called like this:

***var x = document.getElementById( 'myDiv' ).documentOffsetLeft;***
3 years ago
Another way to do that through **concat**

***var arr = [1, 2, 3, 4, 5, 6, 7];
console.log([0].concat(arr));***

The difference between **concat** and **unshift** is that **concat** returns a new array. The performance between them could be found here.

***function fn_unshift() {
arr.unshift(0);
return arr;
}

function fn_concat_init() {
return [0].concat(arr)
}***
3 years ago
There's no built-in ability to **break** in **forEach**. To interrupt execution you would have to throw an exception of some sort. eg.

***var BreakException = {};

try {
[1, 2, 3].forEach(function(el) {
console.log(el);
if (el === 2) throw BreakException;
});
} catch (e) {
if (e !== BreakException) throw e;
}***

JavaScript exceptions aren't terribly pretty. A traditional for loop might be more appropriate if you really need to break inside it.

Use Array#some

***[1, 2, 3].some(function(el) {
console.log(el);
return el === 2;
});***

This works because **some** returns **true** as soon as any of the callbacks, executed in array order, return true, short-circuiting the execution of the rest.

**some**, its inverse **every** (which will stop on a **return false**), and forEach are all ECMAScript Fifth Edition methods which will need to be added to the **Array.prototype** on browsers where they're missing.
3 years ago
If you use foreach iterate $json_a alone, you have an object of objects. Even if you pass in true as the second parameter, you have a two-dimensional array. If you're looping through the first dimension you can't just echo the second dimension like that. So this is wrong:

***foreach ($json_a as $k => $v) {
echo $k, ' : ', $v;
}***

To echo the statuses of each person, try this:

***
$string = file_get_contents("/home/michael/test.json");
if ($string === false) {
// deal with error...
}

$json_a = json_decode($string, true);
if ($json_a === null) {
// deal with error...
}

foreach ($json_a as $person_name => $person_a) {
echo $person_a['status'];
}

?>***
3 years ago
JSON.stringify()

JSON.stringify() serializes a JS object or value into a JSON string.

***JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify('foo'); // '"foo"'
JSON.stringify([1, 'false', false]); // '[1,"false",false]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5))
// '"2006-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}' or '{"y":6,"x":5}'
JSON.stringify([new Number(1), new String('false'), new Boolean(false)]);
// '[1,"false",false]'
JSON.parse()***

The JSON.parse() method parses a string as JSON, optionally transforming the value produced.

***JSON.parse('{}'); // {}
JSON.parse('true'); // true
JSON.parse('"foo"'); // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null'); // null***
3 years ago