改造mapus过程中学习到JavaScript语言技巧

📅 2026/7/26 19:06:32
改造mapus过程中学习到JavaScript语言技巧
好多方法要求是对象而对于数组则不适用。1.判断对象是否为空function isEmptyObj(obj) { return Object.keys(obj).length 0 } console.log(对象是否为空, isEmptyObj({}))2.Object.keys用法要求是对象不能是数组3.async和await4.firebase里的push setdb.ref(rooms/room/objects/currentid/coords/).push({ set:[lat,lng] })直接用supabase代替const { error:error1 } await _supabase .from(coords) .insert({ objects_id: data[0].id, lat: lat, lng: lng });5.$.grep用法objects $.grep(objects, function(e){ return e.id ! inst.id; });6.objects.find用法const id $(this).attr(data-id);// 这里取出的是带双引号的字符型id // const inst objects.find(x x.id id);// 而objects的id是数值型用永远查不到 const inst $.grep(objects, function(e){ return e.id id; })[0];7.数据结构不同需要调整var coords []; Object.values(snapshot.coords).forEach(function(coord){ // coords.push([coord.set[0], coord.set[1]]); coords.push([coord.lat, coord.lng]); });8.Object.values用法要求是对象if (snapshot.val() ! null) { Object.values(snapshot.val()).forEach(function(cursor, index){ renderCursors(cursor, Object.keys(snapshot.val())[index]); }) }要求是对象不能是数组如果是数组就要if (data2.length ! 0) { data2.forEach(function(object, index){ renderCursors(cursors, object.id); }); }9.$.inArray(value,array)用法var objectsid []; data4.forEach(function(object, index){ objectsid.push(object.id); }) // if ($.inArray(inst.id, Object.keys(snapshot.val())) -1) { if ($.inArray(inst.id, objectsid) -1) { // 改成这样