gpt4 book ai didi

javascript - JSON 和 Angularjs

转载 作者:行者123 更新时间:2023-11-30 16:12:33 25 4
gpt4 key购买 nike

我试图在 JSON 中循环一些产品。我得到了许多不同类型的 JSON 文件,这就是为什么我制作了可以手动“修补”文件的形式。

{ "product": [ { "@attributes": { "ID": "123456789" }, "name": "Name of the product"],...}

所以我的表单中有两个输入字段:

1. id="product_name" value="name"
2. id="product_id" value="['@attributes']['ID']"

我使用 ng-repeat 循环遍历产品。这些工作正常:

{{product}} //Product object
{{product.name}} //name of the product
{{product[value]}} //if value = name, name of the product

我的问题是我不知道如何从产品中获取 ['@attribute']['ID']。

编辑:我知道这会起作用:

{{product['@attributes']['ID']}}

但我需要更改表单输入的值。

编辑:解决方案:

controller.getData = function(object, key) {
var keys = [];
var count = key.replace(/[^.]/g, '').length;
if(count === 4){
keys = key.split(".");
return object[keys[0]][keys[1]][keys[2]][keys[3]][keys[4]];
}else if(count === 3){
keys = key.split(".");
return object[keys[0]][keys[1]][keys[2]][keys[3]];
}else if(count === 2){
keys = key.split(".");
return object[keys[0]][keys[1]][keys[2]];
}else if(count === 1){
keys = key.split(".");
return object[keys[0]][keys[1]];
}else{
return object[key];
}
};

新问题:

{ "product": [ 
{ "@attributes": { "ID": "12345" },
"name": "productname",
"price": "xx",
"URL": "url",
"images": { "image": "imgUrl" },
"description": {},
"categories": {
"category": "Kesäale" },
"properties": { "property": [
{ "@attributes": { "name": "color" }, "value": "B25 Grisaille" },
{ "0": "\n", "@attributes": { "name": "size" } },
{ "@attributes": { "name": "currency" }, "value": "EUR" },
{ "@attributes": { "name": "brand" }, "value": "brandName" },
{ "@attributes": { "name": "fromPrice" }, "value": "xx" },
{ "@attributes": { "name": "manufacturer" }, "value": "xx" },
{ "@attributes": { "name": "weight" }, "value": "0.5" },
{ "@attributes": { "name": "stock" }, "value": "true" },
{ "@attributes": { "name": "EAN" }, "value": "1234" } ] },
"variations": {} },

如何获取品牌名称?

最佳答案

这是属性中一些特殊字符的问题。如果您有一个 json key ,例如my-key 无法以属性方式访问。因此,您可以使用后备数组访问来获取值:

var x = { "product": [ { "@attributes": { "ID": "123456789" }, "name": "Name of the product"}]}

// [0] not required when looping through the list...
var id = x.product[0]["@attributes"].ID

console.log(id); // 123456789

编辑:要动态读取它,您可以像这样使用一些辅助函数(如果路径更动态,可以扩展)

var key = "@attributes.ID";

function getData(object, key) {
var keys = key.split(".");
return object[keys[0]][keys[1]];
}

// sample call for demo purpose
var res = getData(x.product[0], key);
console.log(res);

然后从您的表达式中调用 getData() 方法,如下所示:

{{ getData(product, value) }} 

EDIT2:对于完全动态的结构:

function getDataDyn(object, keystr) {
var keys = keystr.split(".");
return digg(object, keys);
}

function digg(obj, keys) {
if(keys.length === 1) {
return obj[keys[0]]
} else {
return digg(obj[keys[0]], keys.splice(1));
}
}

// sample call for demo purpose
var result = getDataDyn(x.product[0], key);
console.log(result);

关于javascript - JSON 和 Angularjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36023276/

25 4 0
文章推荐: javascript - 为什么使用 Webpack 和 Gulp 构建的输出包这么大(>1.9Mb)?
文章推荐: c - `nm` 输出符号未被 `LC_SYMTAB` 引用
文章推荐: c - 按位运算 : Need to create a large integer, 但只能声明一个8bit整数
文章推荐: javascript - 如何从
中获取数据并在单独的
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com