gpt4 book ai didi

javascript - 如何根据对象的 "blueprints"将给定对象与类别匹配

转载 作者:行者123 更新时间:2023-11-30 18:09:45 25 4
gpt4 key购买 nike

我正在尝试对从客户那里收到的对象进行分类。

在服务器端,我定义了我的“蓝图”:

{ // "type1"
type: 1,
name: String,
password: String
}

{ // "type2"
type: 2,
user_id: Number,
action: String
}

{ // "type3", and yes, this says type: 2....
type: 2,
object_id: Number,
action: String
}

根据客户端发送的内容,我想将它们分类如下:

{ type: 1, name: 'user', password: 'pass' }                    // -> type1
{ type: 1, name: 'user', password: 'pass', remember_me: true } // -> type1
{ type: 2, name: 'user', password: 'pass' } // -> N/A
{ type: 2, user_id: 5, action: 'hello' } // -> type2
{ type: 2, object_id: 5, action: 'hello' } // -> type3

标识需要基于键名、值的数据类型和值的实际值。每秒将发送数千个对象,并且可能有数千个蓝图。因此,如果能在< O(n)中完成就好了。其中 n 是蓝图的数量。

我正在从头开始写这篇文章,这样蓝图和元数据就可以存储在任何需要的数据结构中。

感谢您的帮助。我期待听到这方面的想法。

最佳答案

关于可能降低复杂性的方法的随机想法:

这里真正的限制因素是您可以在多大程度上减少类型集。最明显的方法之一是仅基于对象的键来执行某些操作。数据中有额外键的问题是我们不能只依赖 Object.keys( data ).sort().join(","),我们还必须尝试每一种组合我们确实有 key 。

// Assuming the "types" list is called "types":
// using underscore.js api
var _ = require('underscore');
var keyMap = _.chain( types ).map(function( typeDef, typeIndex ) {
// get an index with the definition, in case its
return { index: typeIndex, def: typeDef };
}).groupBy(function( data ) {
return _.keys( data.def ).sort().join(",");
}).value();

// empty map needed
keyMap[""] = [];

// assumes sorted key list
function getPossibleMaps( keys ) {
// if we have a map for this, use it
if ( keyMap[ keys.join(",") ] ) {
return keyMap[ keys.join(",") ];
} else {
// create a map of possible types by removing every key from the list of keys
// and then looking for maps that match, cache our result
return keyMap[ keys.join(",") ] = recursiveMapTest( keys );
}
}

function recursiveMapTest( keys ) {
return _.chain( keys )
.map(function( key ) {
return getPossibleMaps( _.without( keys, key ) );
}).flatten().value();
}

// we must also include "lesser" definitions for each of the key lists we found:
_.each( keyMap, function( results, index ) {
var keys = index.split(",");
keyMap[index] = results.concat( recursiveMapTest( keys ) );
});

function getType( data ) {
function checkType( typeData ) {
var def = typeData.def;
return _.every(typeData.def, function( value, key ) {
// these checks are probably not quite right
if ( value === null ) {
return true;
} else if ( value === Number ) {
return typeof data[key] === "number" || data instanceof Number;
} else if ( value === String ) {
return typeof data[key] === "string" || data instanceof String;
} else {
return data[ key ] === value;
}
});
}
var match = _.find( getPossibleMaps( _.keys( data ).sort() ), checkType );
return match && match.index;
}

// Retrieve
var clientTypes = [
{ type: 1, name: 'user', password: 'pass' },
{ type: 2, name: 'user', password: 'pass' },
{ type: 2, user_id: 5, action: 'hello' },
{ type: 2, object_id: 5, action: 'hello' },
{ type: 1, name: 'user', password: 'pass', remember_me: true }
];

console.log('Client types:');
for (var i = 0; i < clientTypes.length; i++) {
var type = clientTypes[i];
// The type object from the map
console.log("getType", type, getType(type));
}

jsbin

当然,这仅意味着可能的传入键列表越多,您存储“快速”查找表所消耗的内存就越多。


此外,如果所有内容都有数字类型,您显然可以使用它来加速该子类型中大量可能的“对象类型”。


我认为您最好的选择是首先避免需要执行任何这些操作。为您的对象传递更好的类型提示。

关于javascript - 如何根据对象的 "blueprints"将给定对象与类别匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14887815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com