gpt4 book ai didi

javascript - 动态更改数据键名来自 JSON AngularJS

转载 作者:行者123 更新时间:2023-11-30 14:34:20 25 4
gpt4 key购买 nike

我有一个 multi-select 是我用 angular-ui 制作的。我想添加一个用户可以更改 JSON 键名的输入,例如,如果用户添加的 API 没有“name”,而是有“firstName”该应用程序更改了“ key 名称”并且可以正常工作。我怎样才能得到这个结果?感谢任何帮助。 我的代码是 here

我真正想要的是:我应该将这个多选作为 widget 添加到名为 bonita studio 的软件中,它应该有一个选项供用户选择任何想要使用的 API 都应该有一个输入字段,用户将选择要迭代的 API 的关键标识符之一。例如,如果用户想要遍历他/她应该能够做的电子邮件而不是名字。我希望这个解释足够了

"use strict";

var app = angular.module("myApp", ['ui.select', 'ngSanitize']);

app.controller("myCtrl", function($scope, $http) {

$scope.headers = "";

var counter = 0;
var chunks = 5;

$scope.datas = [];

$scope.getData = function getData() {
return $http({
method: "GET",
url: "data.json"
})
.then(function onSuccess(response) {
for (let i = 0; i < response.data.length; i++) {
$scope.datas.push(response.data[i]);
}
//counter += chunks;
})
.catch(function onError(response) {
console.log(response.status);
});


}

/*$scope.loadMore = function () {
$http({
method: "GET",
url: "data.json"
})
.then(function loadMoreSuccess(response) {
for (let i = counter; i < (counter + chunks); i++) {
$scope.datas.push(response.data[i]);
}
counter += chunks;
})
.catch(function onError(response) {
console.log(response.status);
});
};*/



$scope.selected = {
value: $scope.datas[0]
};







});
#widgetContainer {
width: 100%;
}

ul li {
list-style: none;
text-align: center;
}

ul {
height: 120px;
overflow-y: auto;
}

#loadMore {
text-align: center;
color: #aaa;
background: #ddd;
cursor: pointer;
}

#category {
text-align: center;
background: #ddd;
}

#listContainer {
width: 20%;
}

span {
cursor: pointer;
}

h4 {
background: #ddd;
color: #aaa;
}
<!DOCTYPE html>
<html lang="en" ng-app="myApp">

<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="script/lib/angular/angular.js"></script>
<script src="https://code.angularjs.org/1.6.10/angular-sanitize.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-select/0.20.0/select.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-select/0.20.0/select.min.css">
<link rel="stylesheet" href="stylesheet/style.css">
</head>

<body ng-controller="myCtrl">
<div class="container">
<input type="text" ng-model="headers">
<ui-select multiple spinner-enabled="true" close-on-select="false" ng-model="selected.value">
<ui-select-no-choice>
couldn't find anything...
</ui-select-no-choice>
<ui-select-match placeholder="Select country...">
<span>{{$item.name}}</span>
</ui-select-match>
<ui-select-choices group-by="'category'" repeat="data in (datas | filter: $select.search) track by $index" refresh="getData($select.search)" refresh-delay="0">
<span>{{data.name}}</span>
</ui-select-choices>
</ui-select>
</div>

<script src="script/script.js"></script>
</body>

</html>

最佳答案

// Supose you have an object with desired change relations:

var relations = { firstName: "name", oldKey: "newKey" };

// Also, you have your dataArray:

var dataArray = [
{ firstName: "foo", oldKey: 1 },
{ firstName: "bar", oldKey: 2 }
];

// Here follows how you can switch keys:

Object.entries(relations).map(([oldKey, newKey]) => {
dataArray.map(data => {
data[newKey] = data[oldKey];
delete data[oldKey];
})
});

// Check out the result:
console.log(dataArray);

关于javascript - 动态更改数据键名来自 JSON AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50654404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com