我需要一个函数isSubset,当给定两个对象时,它会比较它的值并判断一个对象是否是另一个对象的子集。
object1 = { pickUpLocation : {city : 'Hyderabad', state: 'Telangana' }};
object2 = { dist : 322, pickUpLocation: {city : 'Hyderabad', state: 'Telangana' }};
isSubset(object1, object2); //should return true
object3 = { pickUpLocation : {city : 'Chennai', state: 'Telangana' }}
object4 = { dist : 322, pickUpLocation: {city : 'Hyderabad', state: 'Telangana' }}
isSubset(object3, object4) //should return false as city's value is different
答案 0 :(得分:9)
使用Lodash isMatch
_.isMatch({prop: 'object', id: 3}, {prop: 'object'})
在对象和源之间执行部分深度比较,以确定对象是否包含等效的属性值。
答案 1 :(得分:5)
您可以尝试使用isSubset包。
这是真的
isSubset(
{ dist : 322, pickUpLocation: {city : 'Hyderabad', state: 'Telangana' }},
{ pickUpLocation : {city : 'Hyderabad', state: 'Telangana' }}
);
这是假的
isSubset(
{ dist : 322, pickUpLocation: {city : 'Hyderabad', state: 'Telangana' }},
{ pickUpLocation : {city : 'Chennai', state: 'Telangana' }}
);
答案 2 :(得分:4)
使用lodash可以轻松完成。
import _ from 'lodash'
const isSubset = (aSubset, aSuperset) => (
_.every(aSubset, (val, key) => _.isEqual(val, aSuperset[key]))
)
用法:
const object1 = { pickUpLocation: { city: 'Hyderabad', state: 'Telangana' }}
const object2 = { dist: 322, pickUpLocation: { city: 'Hyderabad', state: 'Telangana' }}
isSubset(object1, object2)
答案 3 :(得分:0)
使用lodash的上述答案有限制,并未涵盖所有边缘情况。我刚刚提出了匹配所有场景的解决方案
def move_cursor_event(self, tree_view, step, count, extend_selection):
rect = tree_view.get_cursor_locations()
self.scroll_y(rect.strong.y)
def scroll_y(self, ypos):
vadj = self.scrolled_window.get_vadjustment()
vadj.set_value(ypos-80)
self.scrolled_window.set_vadjustment(vadj)
案例1:
import _ from 'lodash';
isSubset(obj1, obj2) {
let matched = true;
_.forEach(obj1, (value, key) => {
if(!requirements || !_.isEqual(value, obj2[key])) {
matched = false;
return;
}
});
return matched;
}
案例2:
const obj1 = { foo: 'bar' };
const obj2 = { foo: 'bar', baz: 'qux' };
console.log(isSubset(obj1, obj2)); // true
案例3:
const obj1 = { foo: 'bar' };
const obj2 = { foo: 'bar' };
console.log(isSubset(obj1, obj2)); // true
案例4:
const obj1 = { foo: 'bar', baz: 'qux' };
const obj2 = { foo: 'bar'};
console.log(isSubset(obj1, obj2)); // false
案例5:
const obj1 = undefiend;
const obj2 = undefiend;
console.log(isSubset(obj1, obj2)); // true
案例6:
const obj1 = undefiend;
const obj2 = { foo: 'bar'};
console.log(isSubset(obj1, obj2)); // true
答案 4 :(得分:0)
这是一个简单的解决方案:
import _ from "lodash"
// is a subset of b?
function isSubset(a, b, merge = false) {
const assign = merge ? _.merge : _.assign;
var c = assign(_.cloneDeep(b), a);
return _.isEqual(b, c);
}
答案 5 :(得分:0)
也有点晚了,但这是另一种不需要任何外部库的解决方案。我基于@Akshay Kapoor 的回答,但是这个处理日期和定义 valueOf() 的其他对象的相等性:
const isSubset = (superObj, subObj) =>
superObj === subObj ||
typeof superObj === "object" &&
typeof subObj === "object" && (
subObj.valueOf() === superObj.valueOf() /* dates & ? */ ||
Object.keys(subObj).every(k => isSubset(superObj[k], subObj[k]))
);
答案 6 :(得分:-1)
function isSubset(obj1, obj2) {
for (var key in obj2){
if (JSON.stringify(obj2[key]) === JSON.stringify(obj1[key]))
return true;
}
return false;
}
编辑:现在是通用的,但如果想要更通用,您应该看到比较的后续链接 https://stackoverflow.com/a/1144249/5965782
答案 7 :(得分:-1)
英文:
{ "status": "1", "result": [ { "RespAlunos": { "id": "5", "pessoas_id": "8", "pessoas_id1": "9", "created": "2015-09-21 10:25:46", "modified": "2015-09-21 10:25:46" }, "Responsavel": { "id": "8", "nome": "responsavel ", "email": "responsavel @hotmail.com", "tipopessoas_id": "3", "status": "1", "created": "2015-09-21 10:17:17", "modified": "2015-09-21 10:17:17" }, "Aluno": { "id": "9", "nome": "aluno", "email": "aluno@gmail.com", "tipopessoas_id": "1", "status": "1", "created": "2015-09-21 10:18:41", "modified": "2015-09-21 10:18:41" }, "Matricula": { "id": "6", "referencia": "238", "pessoas_id": "9", "turmas_id": "4", "escolas_id": "2", "status": "1", "created": "2015-09-21 10:35:08", "modified": "2016-02-18 10:51:20" }, "Turma": { "id": "4", "descricao": "4º ano", "created": "2015-09-21 10:31:32", "modified": "2015-09-21 10:31:32" }, "Escola": { "id": "2", "descricao": "Santa Luz Unidade 2", "created": "2015-09-17 23:09:38", "modified": "2015-09-17 23:09:38" }, "Agenda": { "id": "34", "data": "2016-02-29 14:40:00", "descricao": "<p>teste 1</p>\r\n", "escolas_id": "2", "turmas_id": "4", "created": "2016-02-29 14:40:21", "modified": "2016-02-29 14:40:21" } }, { "RespAlunos": { "id": "5", "pessoas_id": "8", "pessoas_id1": "9", "created": "2015-09-21 10:25:46", "modified": "2015-09-21 10:25:46" }, "Responsavel": { "id": "8", "nome": "responsavel ", "email": "responsavel @hotmail.com", "tipopessoas_id": "3", "status": "1", "created": "2015-09-21 10:17:17", "modified": "2015-09-21 10:17:17" }, "Aluno": { "id": "9", "nome": "aluno", "email": "aluno@gmail.com", "tipopessoas_id": "1", "status": "1", "created": "2015-09-21 10:18:41", "modified": "2015-09-21 10:18:41" }, "Matricula": { "id": "6", "referencia": "238", "pessoas_id": "9", "turmas_id": "4", "escolas_id": "2", "status": "1", "created": "2015-09-21 10:35:08", "modified": "2016-02-18 10:51:20" }, "Turma": { "id": "4", "descricao": "4º ano", "created": "2015-09-21 10:31:32", "modified": "2015-09-21 10:31:32" }, "Escola": { "id": "2", "descricao": "Santa Luz Unidade 2", "created": "2015-09-17 23:09:38", "modified": "2015-09-17 23:09:38" }, "Agenda": { "id": "27", "data": "2016-02-29 08:24:00", "descricao": "descricao", "escolas_id": "2", "turmas_id": "4", "created": "2016-02-29 08:25:20", "modified": "2016-02-29 08:25:20" } } ] }
中是否有某些键,以确保其值与object2
之间存在深相等的条件?
写出来:
object1
以较短的形式:
_.some( // Is there some key
object2, // in object2
function(val, key) { // which satisfies the condition that
return _.isEqual( // there is deep equality between
val, // its value and
object1[key] // object1?
);
}
);
在ES6中:
_.some(object2, function(val, key) { return _.isEqual(val, object1[key]); })
我的建议是阅读lodash docs并了解所有API,或至少是主要API。例如,如果您不了解_.some(object2, (val, key) => _.isEqual(val, object1[key]))
和_.some
等重要例程,则很难自行解决此问题。另一方面,如果你确实了解它们,那就非常简单了。