我将一些代码放在一起,以展平和展开复杂/嵌套的JSON对象.它有效,但它有点慢(触发'长脚本'警告).
对于我想要的扁平名称"." 作为数组的分隔符和[INDEX].
例子:
un-flattened | flattened
---------------------------
{foo:{bar:false}} => {"foo.bar":false}
{a:[{b:["c","d"]}]} => {"a[0].b[0]":"c","a[0].b[1]":"d"}
[1,[2,[3,4],5],6] => {"[0]":1,"[1].[0]":2,"[1].[1].[0]":3,"[1].[1].[1]":4,"[1].[2]":5,"[2]":6}
Run Code Online (Sandbox Code Playgroud)
我创建了一个基准,可以模拟我的用例http://jsfiddle.net/WSzec/
我想要更快的代码:为了澄清,在IE 9 +,FF 24+和Chrome 29 中完成JSFiddle基准测试(http://jsfiddle.net/WSzec/)的代码明显更快(~20%+会很好) +.
以下是相关的JavaScript代码:当前最快:http://jsfiddle.net/WSzec/6/
JSON.unflatten = function(data) {
"use strict";
if (Object(data) !== data || Array.isArray(data))
return data;
var result = {}, cur, prop, idx, last, temp;
for(var p in data) {
cur = result, prop = "", last = 0;
do {
idx = p.indexOf(".", last);
temp …Run Code Online (Sandbox Code Playgroud) 我不是在问这是否合适:
Object.prototype.method = function(){};
Run Code Online (Sandbox Code Playgroud)
几乎每个人都认为这是邪恶的,因为它会混乱for(var i in obj).
忽略
Object.defineProperty)假设你有一些非常有用的方法,这被认为是错误的/不道德的吗?
Object.defineProperty(Object.prototype, 'methodOnSteriods',{
value: function(){ /* Makes breakfast, solves world peace, takes out trash */ },
writable: true,
configurable: true,
enumerable: false
});
Run Code Online (Sandbox Code Playgroud)
如果你认为上述内容是不道德的,为什么他们甚至会首先实现这个功能呢?
我想在Array.prototype和Object.prototype上定义辅助方法.我目前的计划是做类似的事情:
Array.prototype.find = function(testFun) {
// code to find element in array
};
Run Code Online (Sandbox Code Playgroud)
所以我可以这样做:
var arr = [1, 2, 3];
var found = arr.find(function(el) { return el > 2; });
Run Code Online (Sandbox Code Playgroud)
它工作正常,但如果我在循环中循环数组,for in方法显示为值:
for (var prop in arr) { console.log(prop); }
// prints out:
// 1
// 2
// 3
// find
Run Code Online (Sandbox Code Playgroud)
这将搞砸任何依赖于for in显示值的人(尤其是对象).更高版本的javascript具有内置于数组中的.map和.filter函数,但这些函数不会出现在for in循环中.如何创建更多不会出现在for in循环中的方法?
Speel Javascript中提到了以下功能: Axel Rauschmayer 为程序员提供的深入指南:
function getDefiningObject(obj, propKey) {
obj = Object(obj); // make sure it’s an object
while (obj && !{}.hasOwnProperty.call(obj, propKey)) {
obj = Object.getPrototypeOf(obj);
// obj is null if we have reached the end
}
return obj;
}
Run Code Online (Sandbox Code Playgroud)
正如作者所说,它的目的是"迭代"一个对象的属性链obj[并返回]第一个具有自己的属性的对象propKey,或者null如果没有这样的对象".
我理解这里的整体推理,但我不明白为什么{}.hasOwnProperty.call(obj, propKey)要做而不仅仅是为了做obj.hasOwnProperty(propKey).有任何想法吗?
我试图了解hasOwnProperty()在迭代对象键时使用check的目标。据我所知,在两种情况下,都会为每个对象属性(不是更多,也不是更少)执行迭代:有hasOwnProperty()或没有。例如,在下面的代码中,带hasOwnProperty()检查和不带检查的结果是一样的:
const obj = {
prop1: [],
prop2: {},
prop3: "",
prop4: 0,
prop5: null,
prop6: undefined
}
const resultWithHasOwnProperty = [];
const resultWithoutHasOwnProperty = [];
for(key in obj) {
if(obj.hasOwnProperty(key)) {
resultWithHasOwnProperty.push(obj[key])
}
}
for(key in obj) {
resultWithoutHasOwnProperty.push(obj[key])
}
console.log("Result WITH hasOwnProperty check: ", resultWithHasOwnProperty);
console.log("Result WITHOUT hasOwnProperty check: ", resultWithoutHasOwnProperty);Run Code Online (Sandbox Code Playgroud)
所以我的问题是:为什么以及何时应该使用hasOwnProperty()检查?
我将重新表述我的问题:如果不hasOwnProperty()检查,我们将始终遍历所有现有属性?
请注意,这个问题并不是真正的重复。