JavaScript/object:修订间差异
来自康健生活
小 文字替换 -“{{leftrightflex|”替换为“{{Double-columns|” 标签:ReplaceText 移动版编辑 移动版网页编辑 |
|||
| (未显示同一用户的15个中间版本) | |||
| 第1行: | 第1行: | ||
== 引言 == | == 引言 == | ||
== function == | |||
{| class="wikitable" | |||
|- | |||
| [[/entries|entries]] || [[/keys|keys]] || getOwnPropertyDescriptor | |||
|- | |||
| 示例 || 示例 || 示例 | |||
|- | |||
| 示例 || 示例 || 示例 | |||
|} | |||
== 更改 key == | == 更改 key == | ||
=== 对象 === | === 对象 === | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
Object.keys(obj).forEach( (key) => { | Object.keys(obj).forEach( (key) => { | ||
if (!obj[key]) { // !obj[key]表示为false,值为空时就是判定为false。 | if (!obj[key]) { // !obj[key]表示为false,值为空时就是判定为false。 | ||
| 第14行: | 第24行: | ||
// | // | ||
}</syntaxhighlight> | }</syntaxhighlight> | ||
== 删除一个对象 == | |||
<syntaxhighlight lang="javascript"> | |||
result.list.map(o=>{ | |||
let {id, ...tmp} = o | |||
return {o.voteStatus} | |||
})</syntaxhighlight> | |||
=== 数组对象 === | === 数组对象 === | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
var array = [ | var array = [ | ||
{ | { | ||
| 第34行: | 第50行: | ||
__proto__: Array(0) | __proto__: Array(0) | ||
</pre>}}}} | </pre>}}}} | ||
== 排序 == | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
const foo = {b:1, a: 2}; | |||
Object.keys(foo).sort().reduce((r, k) => (r[k] = foo[k], r), {}); | |||
</syntaxhighlight>|{{consoleout|<pre> | |||
{a: 2, b: 1} | |||
</pre>}}}} | |||
深度递归实现: | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
const comparable = o => (typeof o != 'object' || !o) | |||
? o | |||
: Object.keys(o).sort().reduce((c, key) => (c[key] = comparable(o[key]), c), {});</syntaxhighlight>|{{consoleout|<pre> | |||
</pre>}}}} | |||
=== 序列化 === | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
const foo = {b:1, a: 2}; | |||
Object.keys(foo).sort().reduce((r, k, i) => (r = r + (i ? '&' : '') + k + '=' + foo[k], r), '?'); | |||
Object.keys(foo).sort().map(k => | |||
encodeURIComponent(k) + '=' + encodeURIComponent(foo[k]) | |||
).join('&');</syntaxhighlight>|{{consoleout|<pre> | |||
?a=2&b=1' | |||
</pre>}}}} | |||
== 拷贝 == | |||
深拷贝、浅拷贝 | |||
=== JSON === | |||
<code>JSON.stringify</code> 不适用于具有循环引用的对象。 | |||
== 等值 == | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
// - 比较两个 json 对象知否相等 | |||
export const objectEqual = function objectEqual (source, target) { | |||
const sou = Object.getOwnPropertyNames(source); | |||
const tar = Object.getOwnPropertyNames(target); | |||
if (sou.length !== tar.length) return false; | |||
for (let i = 0, max = sou.length; i < max; i++) { | |||
const name = sou[i]; | |||
if (source[name] !== target[name]) return false; | |||
} | |||
return true; | |||
};</syntaxhighlight>|{{consoleout|<pre> | |||
</pre>}}}} | |||
=== deep Equal === | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
const deepEqual = (x, y) => { | |||
const ok = Object.keys, tx = typeof x, ty = typeof y; | |||
return x && y && tx === 'object' && tx === ty | |||
? (ok(x).length === ok(y).length && ok(x).every(key => deepEqual(x[key], y[key]))) | |||
// :( x === y &&(x!= null && y!= null || x.constructor === y.constructor)) | |||
: (x === y); | |||
}</syntaxhighlight>|{{consoleout|<pre> | |||
</pre>}}}} | |||
== 参考 == | |||
* https://github.com/ReactiveSets/toubkal/blob/master/lib/util/value_equals.js | |||
2026年11月26日 (四) 08:06的最新版本
引言
function
| entries | keys | getOwnPropertyDescriptor |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
更改 key
对象
Object.keys(obj).forEach( (key) => {
if (!obj[key]) { // !obj[key]表示为false,值为空时就是判定为false。
delete(obj[key]) // 删除对象的key。
}
})
for (prop in myObject){
//
}
删除一个对象
result.list.map(o=>{
let {id, ...tmp} = o
return {o.voteStatus}
})
数组对象
var array = [
{
id:1,
name:"小明"
},
{
id:2,
name:"小红"
}
];
var result = array.map(o=>{return{value:o.id, label:o.name}});
console.log(result);
0: {value: 1, label: "小明"}
1: {value: 2, label: "小红"}
length: 2
__proto__: Array(0)
排序
const foo = {b:1, a: 2};
Object.keys(foo).sort().reduce((r, k) => (r[k] = foo[k], r), {});
{a: 2, b: 1}
深度递归实现:
const comparable = o => (typeof o != 'object' || !o)
? o
: Object.keys(o).sort().reduce((c, key) => (c[key] = comparable(o[key]), c), {});
序列化
const foo = {b:1, a: 2};
Object.keys(foo).sort().reduce((r, k, i) => (r = r + (i ? '&' : '') + k + '=' + foo[k], r), '?');
Object.keys(foo).sort().map(k =>
encodeURIComponent(k) + '=' + encodeURIComponent(foo[k])
).join('&');
?a=2&b=1'
拷贝
深拷贝、浅拷贝
JSON
JSON.stringify 不适用于具有循环引用的对象。
等值
// - 比较两个 json 对象知否相等
export const objectEqual = function objectEqual (source, target) {
const sou = Object.getOwnPropertyNames(source);
const tar = Object.getOwnPropertyNames(target);
if (sou.length !== tar.length) return false;
for (let i = 0, max = sou.length; i < max; i++) {
const name = sou[i];
if (source[name] !== target[name]) return false;
}
return true;
};
deep Equal
const deepEqual = (x, y) => {
const ok = Object.keys, tx = typeof x, ty = typeof y;
return x && y && tx === 'object' && tx === ty
? (ok(x).length === ok(y).length && ok(x).every(key => deepEqual(x[key], y[key])))
// :( x === y &&(x!= null && y!= null || x.constructor === y.constructor))
: (x === y);
}
参考
No categories