简体   繁体   English

这两个javascript对象有什么区别

[英]What's the difference between this two javascript objects

I am trying to get values from the following object. 我试图从以下对象获取值。 The for loop works in one of the objects but won't in the other javascript object. for循环可在其中一个对象中使用,而不能在另一个javascript对象中使用。 I was wondering what the difference and how can I get it to work in the other object? 我想知道有什么区别,如何使它在另一个对象中工作?

Object 1: 对象1:

var objects = [
  {
    "foo" : "bar",
    "bar" : "sit"
  },
  {
    "foo" : "lorem",
    "bar" : "ipsum"
  }
];

Object 2: 对象2:

{
"4dd5a49e366": {
"name" : "bar",
"bar" : "sit",
"date": "2016-08-03T04:48:04.283Z"
},
"519c5056af2": {
"name" : "lorem",
"bar" : "ipsum",
"date": "2016-09-03T04:48:04.283Z"
}
}

I want to do a search for items where name attribute is matching some search_term. 我想搜索名称属性与某些search_term匹配的项目。 And return the items. 并退还物品。

Here is the search for loops am using. 这是正在使用的搜索循环。

function searchFor(toSearch) {
      var results = [];
      toSearch = trimString(toSearch); // trim it
      for(var i=0; i<objects.length; i++) {
        for(var i in objects[i]) {
          if(objects[i][key].indexOf(toSearch)!=-1) {
            if(!itemExists(results, objects[i])) results.push(objects[i]);
          }
        }
      }
      return results;
    }
   console.log(searchFor('o'));

This works for the first object and not for the second. 这适用于第一个对象,不适用于第二个对象。 How can I get it to work for the second? 我如何才能使其第二次工作?

I suggest you do some reading on JavaScript Object literals and Arrays. 我建议您阅读JavaScript Object常量和数组。 The first example is an array of objects. 第一个示例是对象数组。 The second is just an object. 第二个只是一个对象。 Two completely different data structures. 两种完全不同的数据结构。

The first variable is an array of objects. 第一个变量是对象数组。 Since it is an array you can use all array methods on it. 由于它是一个数组,因此可以在其上使用所有数组方法。

Second one is an object with keys 4dd5a49e366 & 519c5056af2 which in turn are again object and have few properties. 第二个对象是具有键4dd5a49e366519c5056af2的对象,它们又是对象并且几乎没有属性。

You cannot use array methods on this second object 您不能在第二个对象上使用数组方法

how can I get it to work in the other object? 如何使它在另一个对象中工作?

Hope this snippet will be useful 希望此片段有用

var myObject = {
  "4dd5a49e366": {
    "name": "bar",
    "bar": "sit",
    "date": "2016-08-03T04:48:04.283Z"
  },
  "519c5056af2": {
    "name": "lorem",
    "bar": "ipsum",
    "date": "2016-09-03T04:48:04.283Z"
  }
}
// a function to accept the name value
function findByName(name) {
  var thisObject = "";
  for (var keys in myObject) { // looping over objects
    var getThisObject = myObject[keys];
    if (getThisObject.name === name) { // Checking if name matches
      thisObject = myObject[keys]; // assigning the object to a variable
    }
  }
  return thisObject // return that variable
}
var getMyObject = findByName('bar');
console.log(getMyObject)

JSFIDDLE JSFIDDLE

EDIT 编辑

if I enter just findByName('b'); 如果我只输入findByName('b'); it should return results that the full name 它应该返回全名的结果

You need to use indexOf to find if this name value contains the specific character. 您需要使用indexOf来查找此名称值是否包含特定字符。

Use an array to store all the relevant object where the name value contains this specific character.Return that array from the function. 使用数组来存储名称值包含此特定字符的所有相关对象,然后从函数中返回该数组。

function findByName(name) {
  var thisObject = [];
  for (var keys in myObject) {
    var getThisObject = myObject[keys];
    if (getThisObject.name.indexOf(name)!==-1) {
      thisObject.push(myObject[keys]);
    }
  }
  return thisObject
}
var getMyObject = findByName('b');

JSFIDDLE 2 JSFIDDLE 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM