【问题标题】:How to get specific property from another object?如何从另一个对象获取特定属性?
【发布时间】:2019-07-11 10:34:09
【问题描述】:

我有一个 Firebase 数据库,其中有几个对象及其属性。

{
  "products": [{
    "sku": "70741",
    "name": "Name",
    "image": "name.png",
    "quantity": 12,
    "price": {
      "value": 2.10,
      "type": "simple"
    },
    "attributes": {
      "0": "2",
      "1": "4",
      "2": "2|1|3",
      "3": "1|2",
      "4": "2",
      "5": "6|7"
    }
  }
],
  "attributes": [{
    "id": 0,
    "name": "name1",
    "type": "type1"
  },
  {
    "id": 1,
    "name": "name2",
    "type": "type2"
  },
  {
    "id": 2,
    "name": "name3",
    "type": "type1"
  }
],
  "attributesIdValue": [
    "companyName1|companyName2|companyName3|companyName4",
    "typeName1|typeName2|typeName3|typeName4",
    "address1|address2|address3|address4",
    "1|2|3",
    "country1|country2|country3|country4",
    "number1|number2|number3|number4"
  ]}

products.attributes 中的键应该指向 attributes.id 和 attributesIdValue.id,products.attributes 中的 value 应该指向 attributesIdValue,具体取决于它的 id 和 value。

我怎样才能用 vue.js 得到这样的东西: product.attributes.0 应该显示这个产品的companyName product.attributes.1 应该显示此产品的typeName product.attributes.2 应该显示此产品的address

{{ product.price.value }}工作并显示值,但我无法访问这些属性。

------- 编辑 换句话说,我想要的是这样的东西,而不是上面的:

  "products": [{
    "sku": "70741",
    "name": "Name",
    "image": "name.png",
    "quantity": 12,
    "price": {
      "value": 2.10,
      "type": "simple"
    },
    "attributes": {
      "attributes.0": "attributesIdValue.2",
      "attributes.1": "attributesIdValue.4",
      "attributes.2": "attributesIdValue.2|attributesIdValue.1|attributesIdValue.3",
      "attributes.3": "attributesIdValue.1|attributesIdValue.2",
      "attributes.4": "attributesIdValue.2",
      "attributes.5": "attributesIdValue.6|attributesIdValue.7"
    }
  }
],
  "attributes": [{
    "id": 0,
    "name": "name1",
    "type": "type1"
  },
  {
    "id": 1,
    "name": "name2",
    "type": "type2"
  },
  {
    "id": 2,
    "name": "name3",
    "type": "type1"
  }
],
  "attributesIdValue": [
    "companyName1|companyName2|companyName3|companyName4",
    "typeName1|typeName2|typeName3|typeName4",
    "address1|address2|address3|address4",
    "1|2|3",
    "country1|country2|country3|country4",
    "number1|number2|number3|number4"
  ]}

这会输出那些里面的数据。

【问题讨论】:

  • 您是否尝试访问attributesIdValueattributes
  • btw 产品和attributesIdValue 的关系是什么?你怎么知道应该使用product.attributes.1 返回哪个typeName (typeName1| typeName2| typeName3)?
  • @SimonThiel 两者。 product.attributes 键(例如“0”:“2”,其中 0 是键,2 是值)应该让我从 attributes.id 得到一个值,即 0product.attributes 值应该让我从0位置开始的第二个值是companyName2@aquilesb我不确定我说的是否清楚,但productattributesattributesIdValue之间应该有联系。 product.attributes.1 应该返回 "1": "4", 所以在这种情况下,key 会得到我 "id": 1, "name": "name2", "type": "type2" 而 value 会得到我 typeName4

标签: json vue.js


【解决方案1】:

你试过 JSON.parse(data);

例子:

var data = '{ "products": [{ "sku": "70741", "name": "Name", "image": "name.png", "quantity": 12, "price": { "value": 2.10, "type": "simple" }, "attributes": { "0": "2", "1": "4", "2": "2|1|3", "3": "1|2", "4": "2", "5": "6|7" } } ], "attributes": [{ "id": 0, "name": "name1", "type": "type1" }, { "id": 1, "name": "name2", "type": "type2" }, { "id": 2, "name": "name3", "type": "type1" } ], "attributesIdValue": [ "companyName1|companyName2|companyName3|companyName4", "typeName1|typeName2|typeName3|typeName4", "address1|address2|address3|address4", "1|2|3", "country1|country2|country3|country4", "number1|number2|number3|number4" ]}';


obj = JSON.parse(data);

console.log(obj.attributes[0].name);

【讨论】:

  • 你为什么要在这个问题上使用 jquery? JSON.parse 不需要 jQuery
  • 大声笑,我只是在你评论之前删除它:D 是错误导入的
  • 我明白了 - 您似乎仍然错过了这里的真正问题。数据已经是一个对象。这是关于动态访问正确的属性值
  • 正如弗兰克所说。这些对象的属性应该以某种方式相互引用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-22
  • 1970-01-01
相关资源
最近更新 更多