首页 \ 问答 \ 使用AngularJS从JSON键和值中删除前导空格(Removing Leading Space from JSON Keys and Values Using AngularJS)

使用AngularJS从JSON键和值中删除前导空格(Removing Leading Space from JSON Keys and Values Using AngularJS)

我将在前面加上这样一个事实,即我得到的JSON来自angular-csv-import,我正在尝试清理键中的字符串,值。

我已经读过这个帖子,但我不想使用JQuery,所以我编辑的方式更加棱角分明。

删除对象键和值中的前导和尾随空格

这是我从CSV文件中获取的数据

[
 {
"TRAIN_LINE":"El",
" ROUTE_NAME":"Brown Line",
" RUN_NUMBER":"E102",
" OPERATOR_ID":"SJones"
 },
 {
"TRAIN_LINE":"Metra",
" ROUTE_NAME":"UPN",
" RUN_NUMBER":"M405",
" OPERATOR_ID":"AJohnson"
 }
]

要将数据发送到我的api,我需要它没有前导或尾随空格来最小化清理并保持一致,无论上传的csv文件是什么。

我能够在键和值上使用.trim()删除空格,但它不会在我要发送到服务器的数据集中更新。 任何帮助非常感谢。 这是我的控制器中的代码。

$scope.postData = function (result) {
    angular.forEach(result, function (index) {     
        angular.forEach(index, function (k, v) {
            k = k.trim();
            v = v.trim();
            return k;
            return v;       
        });
        return index;
        console.log(index);
    });
    console.log(result);     
};

请注意,如果我记录k和v删除了空格,但是如何将其推送到结果中。


I'm going to preface this with the fact that the JSON I am getting is coming from angular-csv-import and I am trying to clean up the strings within the keys, values.

I have read this thread but I don't want to to use JQuery so I edited to be in a more angular way.

Remove leading and trailing spaces in object keys and values

Here is the data I am getting from the CSV file

[
 {
"TRAIN_LINE":"El",
" ROUTE_NAME":"Brown Line",
" RUN_NUMBER":"E102",
" OPERATOR_ID":"SJones"
 },
 {
"TRAIN_LINE":"Metra",
" ROUTE_NAME":"UPN",
" RUN_NUMBER":"M405",
" OPERATOR_ID":"AJohnson"
 }
]

To send the data to my api I need it to have no leading or trailing spaces to minimize clean up and keep it consistent no matter what the csv file uploaded is.

I'm able to remove the space using .trim() on the keys and values but it is not updating in the dataset I am going to send to the server. Any help much appreciated. Here is my code in my controller.

$scope.postData = function (result) {
    angular.forEach(result, function (index) {     
        angular.forEach(index, function (k, v) {
            k = k.trim();
            v = v.trim();
            return k;
            return v;       
        });
        return index;
        console.log(index);
    });
    console.log(result);     
};

Note if I log k and v the spaces are removed, but how do I push that to the results.


原文:https://stackoverflow.com/questions/30429077
更新时间:2022-04-13 13:04

最满意答案

我想通了......显然在可拖动功能中stack的使用已经改变:

http://api.jqueryui.com/draggable/#option-stack

它不再需要一组变量,而是一个变量。 将变量更改为更符合新用法,修复了错误。


I figured it out... apparently the usage of stack in the draggable function has changed:

http://api.jqueryui.com/draggable/#option-stack

It no longer takes a set of variables, but a single variable. Changing the variable to be more in-line with the new usage fixed the error.

相关问答

更多
  • 使用“遏制”选项: http://docs.jquery.com/UI/Draggable#option-containment 文档说它只接受值:'parent','document','window',[x1,y1,x2,y2],但我似乎记得它也会接受一个选择器,如'#container'。 Use the "containment" option: jQuery UI API - Draggable Widget - containment The documentation says it only ...
  • 我发现问题是,我只是简单地链接到jQuery UI,就像我在没有CMS的网站上一样。 但是,在网站建立在Wordpress上的情况下,我应该正确排列脚本。 关于如何执行此操作的Wordpress主题: https : //developer.wordpress.org/reference/functions/wp_enqueue_script/ 虽然为了简化在构建的这个阶段,并澄清它的工作原理,我使用了这个插件: https : //wordpress.org/plugins/enqueue-me/ I'v ...
  • 看看http://jsfiddle.net/snowMonkey/7yEaU/ $("table tbody tr").each(function () { var that=this; $(this).droppable({ accept: ".draggable", over: function (e, ui) { // add class 'accept-incoming-task' to the row under after ...
  • http://jqueryui.com/draggable/#snap-to 将可拖动的捕捉到DOM元素的内部或外部边界。 使用snap,snapMode(inner,outer,both)和snapTolerance(调用捕捉时,draggable必须来自元素的距离,以像素为单位)选项。 http://jqueryui.com/draggable/#snap-to Snap the draggable to the inner or outer boundaries of a DOM element. U ...
  • 我想通了......显然在可拖动功能中stack的使用已经改变: http://api.jqueryui.com/draggable/#option-stack 它不再需要一组变量,而是一个变量。 将变量更改为更符合新用法,修复了错误。 I figured it out... apparently the usage of stack in the draggable function has changed: http://api.jqueryui.com/draggable/#option-stack ...
  • 尝试 var xPos = $(this).css('left'); var yPos = $(this).css('top'); ...代替。 Try var xPos = $(this).css('left'); var yPos = $(this).css('top'); ...instead.
  • 您无法使用#启动ID。 你在这里: var newId = '#draggable_'+randomnumber; 它应该是: var newId = 'draggable_'+randomnumber; 然后这个: $(newId).draggable(); 应该: $('#' + newId).draggable(); 示例: http : //jsfiddle.net/M3UWp/ 所以你最终得到: var newId = 'draggable_'+randomnumber; $(ui.hel ...
  • $(document).ready(function() { $("#draggableDiv").draggable(); }); .divClass { width: 200px; height: 200px; background-color: Blue; color: White; vertical-align: middle; text-align: center; display: table-cell; } #draggableDiv { ...
  • 尝试: // selector means "doesn't have", ':not(:has(selector))' $(".ui-draggable:not(:has(.ui-draggable-disabled))").hover(function() { ... 要么: $('.ui-draggable').not(':has(.ui-draggable-disabled)').hover(function() { 或者在hover鼠标悬停/ mouseout函数中测试是否存在已禁用的类: $ ...
  • DEMO 试试这个,只需在你的textarea id="text"添加一个id属性,然后在你的draggable()添加一个cancel:"text,"属性cancel:"text," 。 HTML